private IExceptionMapper GetExceptionMapperFor(OpenApiMappedException mappedException, Exception ex, OpenApiOperation operation) { if (mappedException?.FormatterType != null) { return(this.exceptionMappers.Value.FirstOrDefault(x => x.GetType() == mappedException.FormatterType)); } return(this.exceptionMappers.Value.FirstOrDefault(x => x.CanMapException(operation.Responses, ex, mappedException?.StatusCode))); }
/// <inheritdoc/> public OpenApiResult GetResponse(Exception exception, OpenApiOperation operation) { Type exceptionType = exception.GetType(); OpenApiMappedException mappedException = this.GetMappedException(operation, exceptionType); IExceptionMapper mapper = this.GetExceptionMapperFor(mappedException, exception, operation); if (mapper == null) { string operationId = operation.GetOperationId(); this.logger.LogError( "Unmapped exception thrown by [{operationId}]: [{exception}]", operationId, exception); throw new OpenApiServiceMismatchException($"Operation {operationId} has thrown an exception of type {exception.GetType().FullName}. You should either handle this in your service, or add an exception mapping defining which status code this exception corresponds to."); } return(mapper.MapException(operation.Responses, exception, mappedException?.StatusCode)); }