private async Task HandleErrorAsync(HttpContext context, Exception exception)
        {
            var exceptionReponse = _mapper.Map(exception);

            context.Response.StatusCode  = (int)exceptionReponse.StatusCode;
            context.Response.ContentType = "application/json";
            await context.Response.WriteAsync(JsonConvert.SerializeObject(exceptionReponse));
        }
        private Task HandleErrorAsync(HttpContext context, Exception exception)
        {
            var response = _exceptionToResponseMapper.Map(exception);
            var payload  = JsonConvert.SerializeObject(response);

            context.Response.ContentType = "application/json";
            context.Response.StatusCode  = (int)response.StatusCode;

            return(context.Response.WriteAsync(payload));
        }
    private async Task HandleErrorAsync(Exception exception, HttpContext context)
    {
        var errorResponse = _mapper.Map(exception);

        context.Response.StatusCode = (int)(errorResponse?.StatusCode ?? HttpStatusCode.InternalServerError);
        var response = errorResponse?.Response;

        if (response is null)
        {
            return;
        }
        await context.Response.WriteAsJsonAsync(response);
    }
        private async Task HandleErrorAsync(HttpContext context, Exception exception)
        {
            var exceptionResponse = _exceptionToResponseMapper.Map(exception);

            context.Response.StatusCode = (int)(exceptionResponse?.StatusCode ?? HttpStatusCode.BadRequest);
            var response = exceptionResponse?.Response;

            if (response is null)
            {
                await context.Response.WriteAsync(String.Empty);
            }

            context.Response.ContentType = "application/json";
            await _jsonSerializer.SerializeAsync(context.Response.Body, exceptionResponse.Response);
        }
        private async Task HandleErrorAsync(HttpContext context, Exception exception)
        {
            var exceptionResponse = _exceptionToResponseMapper.Map(exception);

            context.Response.StatusCode = (int)(exceptionResponse?.StatusCode ?? HttpStatusCode.InternalServerError);
            var response = exceptionResponse?.Response;

            if (response is null)
            {
                await context.Response.WriteAsync(string.Empty);

                return;
            }

            context.Response.ContentType = "application/json";
            var payload = JsonConvert.SerializeObject(exceptionResponse.Response, SerializerSettings);
            await context.Response.WriteAsync(payload);
        }
        private async Task HandlerErrorAsync(HttpContext context, System.Exception exception)
        {
            var exceptionResponse = _exceptionToResponseMapper.Map(exception);

            context.Response.StatusCode = exceptionResponse is not null
                ? (int)exceptionResponse.StatusCode
                : (int)HttpStatusCode.BadRequest;

            if (exceptionResponse?.Response == null)
            {
                await context.Response.WriteAsync(string.Empty, new CancellationToken());
            }
            else
            {
                context.Response.ContentType = "application/json";
                var text = JsonConvert.SerializeObject(exceptionResponse.Response);
                await context.Response.WriteAsync(text, new CancellationToken());
            }
        }
Beispiel #7
0
        // write the error in format { code: "", reason: "" }
        private async Task HandleErrorAsync(HttpContext context, Exception exception)
        {
            var exceptionResponse = _exceptionToResponseMapper.Map(exception);

            // assign status code depending on the exceptionResponse object
            // if status code was null, assign 500 (IntervalServerError)
            // else statusCode from property
            context.Response.StatusCode = (int)(exceptionResponse?.StatusCode ?? HttpStatusCode.InternalServerError);
            var response = exceptionResponse?.Response;

            if (response is null)
            {
                await context.Response.WriteAsync(string.Empty);
            }

            context.Response.ContentType = "application/json";
            // serializes to string
            var payload = JsonConvert.SerializeObject(exceptionResponse.Response);
            // write to the response body
            await context.Response.WriteAsync(payload);
        }
    private async Task HandleErrorAsync(HttpContext context, System.Exception exception)
    {
        var exceptionResponse = await _exceptionToResponseMapper.Map(exception);

        if (exceptionResponse is null)
        {
            _logger.LogWarning(exception, "{message}", exception.Message);
            await context.Response.WriteAsync(string.Empty);

            return;
        }
        var statusCode = (int)(exceptionResponse?.StatusCode ?? HttpStatusCode.BadRequest);

        _logger.LogException(exception, statusCode);

        context.Response.StatusCode  = statusCode;
        context.Response.ContentType = "application/json";

        var result = Encoding.UTF8.GetBytes(JsonSerializer.Serialize(exceptionResponse?.Response, _serializerOptions));
        await context.Response.Body.WriteAsync(result);
    }