Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom exception types #8

Open
alexyakunin opened this issue Jul 2, 2020 · 3 comments
Open

Add custom exception types #8

alexyakunin opened this issue Jul 2, 2020 · 3 comments
Labels
good first issue Good for newcomers help wanted Extra attention is needed

Comments

@alexyakunin
Copy link
Collaborator

This is a "meta issue": currently I mostly throw exceptions of existing exception types from mscorlib. This isn't good in many cases, i.e. adding a few custom types would definitely make it easier to identify certain error scenarios from user code.

On a positive side, I always throw exceptions via Error.* methods, i.e. it's actually pretty easy to identify all the scenarios that would benefit from custom-typed exceptions & update them accordingly.

@alexyakunin alexyakunin added good first issue Good for newcomers help wanted Extra attention is needed labels Jul 19, 2020
@BokhodirUrinboev
Copy link

Good evening can't we make custom exception middleware and leave it to programmer handle the rest of it
We just need to make initial structure for in server side register it in program.

app.UseMiddleware<ErrorHandlerMiddleware>();

For example we can use some class for custom exception type

` public class HttpResponseException
{
public int? status { get; }
public object? internalError { get; }
public string? message { get; }
public string? code { get; }
public string? name { get; }

    public HttpResponseException(int? status, object? internalError, string? message, string? code, string? name)
    {
        this.status = status;
        this.internalError = internalError;
        this.message = message;
        this.code = code;
        this.name = name;
    }
}`

And write handler for it and leave it to programmer to handle it

` public class ErrorHandlerMiddleware
{
private readonly RequestDelegate _next;
private HttpResponseException httpResponseException { get; set; }
public ErrorHandlerMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task Invoke(HttpContext context)
{
try
{
await _next(context);
}
catch (Exception ex)
{
var response = context.Response;
response.ContentType = "application/json";

            switch (ex.Message)
            {
                case "err-404":
                    httpResponseException = new HttpResponseException(status: 400, internalError: ex.InnerException,
                        message: "Entity not found", code: ex.Message, name: "Bad Request");
                    break;
                default:
                    httpResponseException = new HttpResponseException(status: 500, internalError: ex.InnerException,
                        message: ex.Message, code: ex.Message, name: "Internal Server Error");
                    break;
            }

            var result = JsonSerializer.Serialize(httpResponseException);
            response.StatusCode = (int)httpResponseException.status;
            await response.WriteAsync(result);
           
        }
    }
}`

So this way we could make custom Exceptions more like json api format ant it would be cleaner than throw 500 and some text
don't u think?

@BokhodirUrinboev
Copy link

And we should probably add it to the template

@alexyakunin
Copy link
Collaborator Author

@BokhodirUrinboev sorry, I noticed this comment very late. This approach certainly works, the "meta issue" was more about adding dedicated exception types for some of errors thrown by Fusion.

And v6 completely overhauled Fusion's communication stack replacing it with Stl.Rpc, so this approach is less necessary there: Stl.Rpc's default is to serialize the exception type & message automatically.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants