diff --git a/src/Squidex/Areas/Api/Config/Swagger/ErrorDtoProcessor.cs b/src/Squidex/Areas/Api/Config/Swagger/ErrorDtoProcessor.cs new file mode 100644 index 000000000..f04099cb5 --- /dev/null +++ b/src/Squidex/Areas/Api/Config/Swagger/ErrorDtoProcessor.cs @@ -0,0 +1,70 @@ +// ========================================================================== +// Squidex Headless CMS +// ========================================================================== +// Copyright (c) Squidex UG (haftungsbeschraenkt) +// All rights reserved. Licensed under the MIT license. +// ========================================================================== + +using System; +using System.Linq; +using System.Threading.Tasks; +using NJsonSchema; +using NSwag; +using NSwag.SwaggerGeneration.Processors; +using NSwag.SwaggerGeneration.Processors.Contexts; +using Squidex.ClientLibrary.Management; +using Squidex.Pipeline.Swagger; + +namespace Squidex.Areas.Api.Config.Swagger +{ + public sealed class ErrorDtoProcessor : IDocumentProcessor + { + public async Task ProcessAsync(DocumentProcessorContext context) + { + var errorSchema = await GetErrorSchemaAsync(context); + + foreach (var operation in context.Document.Paths.Values.SelectMany(x => x.Values)) + { + AddErrorResponses(operation, errorSchema); + + CleanupResponses(operation); + } + } + + private static void AddErrorResponses(SwaggerOperation operation, JsonSchema4 errorSchema) + { + if (!operation.Responses.ContainsKey("500")) + { + operation.AddResponse("500", "Operation failed", errorSchema); + } + + foreach (var (code, response) in operation.Responses) + { + if (code != "404" && code.StartsWith("4", StringComparison.OrdinalIgnoreCase) && response.Schema == null) + { + response.Schema = errorSchema; + } + } + } + + private static void CleanupResponses(SwaggerOperation operation) + { + foreach (var (code, response) in operation.Responses.ToList()) + { + if (string.IsNullOrWhiteSpace(response.Description) || + response.Description?.Contains("=>") == true || + response.Description?.Contains("=>") == true) + { + operation.Responses.Remove(code); + } + } + } + + private Task GetErrorSchemaAsync(DocumentProcessorContext context) + { + var errorType = typeof(ErrorDto); + + return context.SchemaGenerator.GenerateWithReferenceAsync(errorType, Enumerable.Empty(), context.SchemaResolver); + } + } +} diff --git a/src/Squidex/app/framework/utils/error.ts b/src/Squidex/app/framework/utils/error.ts index 72bac2567..48c1c8413 100644 --- a/src/Squidex/app/framework/utils/error.ts +++ b/src/Squidex/app/framework/utils/error.ts @@ -15,6 +15,10 @@ export class ErrorDto { ) { this.displayMessage = formatMessage(message, details); } + + public toString() { + return `ErrorDto(${JSON.stringify(this)})`; + } }