private IActionResult MapExceptionToResult()

in src/Microsoft.Health.Dicom.Api/Features/Exceptions/ExceptionHandlingMiddleware.cs [62:127]


        private IActionResult MapExceptionToResult(Exception exception)
        {
            HttpStatusCode statusCode = HttpStatusCode.InternalServerError;
            string message = exception.Message;

            switch (exception)
            {
                case ValidationException _:
                case NotSupportedException _:
                case AuditHeaderCountExceededException _:
                case AuditHeaderTooLargeException _:
                    statusCode = HttpStatusCode.BadRequest;
                    break;
                case ResourceNotFoundException _:
                    statusCode = HttpStatusCode.NotFound;
                    break;
                case NotAcceptableException _:
                case TranscodingException _:
                    statusCode = HttpStatusCode.NotAcceptable;
                    break;
                case DataStoreException _:
                    statusCode = HttpStatusCode.ServiceUnavailable;
                    break;
                case InstanceAlreadyExistsException _:
                case ExtendedQueryTagsAlreadyExistsException _:
                case ExtendedQueryTagsOutOfDateException _:
                    statusCode = HttpStatusCode.Conflict;
                    break;
                case UnsupportedMediaTypeException _:
                    statusCode = HttpStatusCode.UnsupportedMediaType;
                    break;
                case ServiceUnavailableException _:
                    statusCode = HttpStatusCode.ServiceUnavailable;
                    break;
                case ItemNotFoundException _:
                    // One of the required resources is missing.
                    statusCode = HttpStatusCode.InternalServerError;
                    break;
                case UnauthorizedDicomActionException udae:
                    _logger.LogInformation("Expected data actions not available: {DataActions}", udae.ExpectedDataActions);
                    statusCode = HttpStatusCode.Forbidden;
                    break;
                case DicomServerException _:
                    statusCode = HttpStatusCode.ServiceUnavailable;
                    break;
            }

            // Log the exception and possibly modify the user message
            switch (statusCode)
            {
                case HttpStatusCode.ServiceUnavailable:
                    _logger.LogWarning(exception, "Service exception.");
                    break;
                case HttpStatusCode.InternalServerError:
                    // In the case of InternalServerError, make sure to overwrite the message to
                    // avoid internal message.
                    _logger.LogCritical(exception, "Unexpected service exception.");
                    message = DicomApiResource.InternalServerError;
                    break;
                default:
                    _logger.LogWarning(exception, "Unhandled exception");
                    break;
            }

            return GetContentResult(statusCode, message);
        }