StackSpot.ErrorHandler
1.0.1
dotnet add package StackSpot.ErrorHandler --version 1.0.1
NuGet\Install-Package StackSpot.ErrorHandler -Version 1.0.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="StackSpot.ErrorHandler" Version="1.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add StackSpot.ErrorHandler --version 1.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: StackSpot.ErrorHandler, 1.0.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install StackSpot.ErrorHandler as a Cake Addin #addin nuget:?package=StackSpot.ErrorHandler&version=1.0.1 // Install StackSpot.ErrorHandler as a Cake Tool #tool nuget:?package=StackSpot.ErrorHandler&version=1.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
StackSpot ErrorHandler
Este componente foi projetado para padronizar os retornos de erro das aplicações.
Versões suportadas
- net5.0
- net6.0
Uso
1. Adicione o pacote NuGet StackSpot.ErrorHandler
ao seu projeto.
dotnet add package StackSpot.ErrorHandler
2. Adicione ao seu IApplicationBuilder
no Startup
da aplicação ou Program
.
app.UseErrorHandler();
Implementação
Exceções tratadas
- Utilizando
HttpResponseException
- existe a possibilidade de personalizar a mensagem, bem como definir umHttpStatusCode
. Além do retorno padronizado da api, existe a opção de gravar o log dos erros que estão ocorrendo, através da propriedadelogActive
, caso o valor seja igual afalse
, o log não é gravado. - O
HttpResponseException
possui diversas sobrecargas, sendo possível atribuir valor as propriedades abaixo:
Campo | Descrição |
---|---|
logActive | Campo que permite ativar a gravação de log |
statusCode | HttpStatusCode que será retornado na requisição |
logLevel | Level do log |
message | Mensagem que será retornada na requisição |
exception | Exceção a ser gravada |
Exemplos de uso:
500 - Internal Server Error
throw new HttpResponseException("Ocorreu um erro ao tentar salvar o registro", true);
Resultado:
{
"error_id": "62ec9afe-64ad-46cb-8df0-abfd306cb7dc",
"message": "Ocorreu um erro ao tentar salvar o registro"
}
400 - Bad Request - neste caso acima, o log não foi ativado.
throw new HttpResponseException(HttpStatusCode.BadRequest, "Campo Nome é obrigatório", false);
Resultado:
{
"error_id": "62ec9afe-64ad-46cb-8df0-abfd306cb7dc",
"message": "Campo Nome é obrigatório"
}
400 - Bad Request
throw new HttpResponseException(HttpStatusCode.BadRequest, "Campo Nome é obrigatório", true);
Resultado:
{
"error_id": "62ec9afe-64ad-46cb-8df0-abfd306cb7dc",
"message": "Campo Nome é obrigatório"
}
- Utilizando
HttpResponseObjectException
- este tem o corpotamento similar doHttpResponseException
, possuindo as mesmas possibilidades citadas anteriomente, só que neste caso também podemos receber umHttpRequestMessage
eHttpResponseMessage
como parametros. - O
HttpResponseException
possui diversas sobrecargas, sendo possível atribuir valor as propriedades abaixo:
Exceções não tratadas
Sempre que ocorrer uma exceção não tratada, a exceção é suprimida e o log sempre será registrado. O HttpStatusCode
será 500 - Internal Server Error
e a Api retornará algo como o exemplo abaixo:
Resultado:
{
"error_id": "62ec9afe-64ad-46cb-8df0-abfd306cb7dc",
"message": "An unexpected system error has occurred"
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 is compatible. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net5.0
- Microsoft.AspNetCore.Http (>= 2.2.2)
- Microsoft.AspNetCore.Http.Abstractions (>= 2.2.0)
- Microsoft.AspNetCore.Http.Extensions (>= 2.2.0)
- Microsoft.Extensions.Logging (>= 6.0.0)
- Newtonsoft.Json (>= 13.0.1)
- System.Text.Encodings.Web (>= 6.0.0)
-
net6.0
- Microsoft.AspNetCore.Http (>= 2.2.2)
- Microsoft.AspNetCore.Http.Abstractions (>= 2.2.0)
- Microsoft.AspNetCore.Http.Extensions (>= 2.2.0)
- Microsoft.Extensions.Logging (>= 6.0.0)
- Newtonsoft.Json (>= 13.0.1)
- System.Text.Encodings.Web (>= 6.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.