private Task HandleExceptionAsync(HttpContext context, Exception ex) { _logger.LogError( $"Error while processing request {context.Request.Method} {context.Request.Path}: {ex.Message}"); if (ex is AutoMapperMappingException mappingException && mappingException.InnerException != null) { ex = mappingException.InnerException; } var code = ex switch { ArgumentException _ => HttpStatusCode.BadRequest, BadDataException _ => HttpStatusCode.BadRequest, DataChangedException _ => HttpStatusCode.BadRequest, ForbiddenException _ => HttpStatusCode.Forbidden, InfrastructureException _ => HttpStatusCode.InternalServerError, InvalidDataException _ => HttpStatusCode.BadRequest, NotFoundException _ => HttpStatusCode.NotFound, UnauthorizedException _ => HttpStatusCode.Unauthorized, _ => HttpStatusCode.InternalServerError }; var result = JsonSerializer.Serialize(new { error = ex.Message }); context.Response.ContentType = "application/json"; context.Response.StatusCode = (int)code; return(context.Response.WriteAsync(result)); } }
private char ReadEscapeCode() { int nextCharacter = InputReader.Read(); if(nextCharacter < 0) { throw new MissingDataException("Code following \\", InputReader.Location); } switch((char)nextCharacter) { case 'n': return '\n'; case 't': return '\t'; case 'r': return '\r'; case '\\': return '\\'; case '"': return '"'; default: var e = new BadDataException("Unknown escape character", InputReader.Location); e.Data["character"] = (char)nextCharacter; throw e; } }