Skip to content

Commit 586ca00

Browse files
Error map (#1185)
* Extract mongo code to own assembly. * Error Map.
1 parent d998f65 commit 586ca00

File tree

5 files changed

+48
-6
lines changed

5 files changed

+48
-6
lines changed

backend/extensions/Squidex.Extensions/Actions/Algolia/AlgoliaActionHandler.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public sealed class AlgoliaActionHandler(RuleEventFormatter formatter, IScriptEn
8383
{
8484
AppId = action.AppId,
8585
ApiKey = action.ApiKey,
86-
Content = serializer.Serialize(content, true),
86+
Content = delete ? null : serializer.Serialize(content, true),
8787
ContentId = contentId,
8888
IndexName = indexName
8989
};
@@ -100,7 +100,6 @@ protected override async Task<Result> ExecuteJobAsync(AlgoliaJob job,
100100
}
101101

102102
var index = await clients.GetClientAsync((job.AppId, job.ApiKey, job.IndexName));
103-
104103
try
105104
{
106105
if (job.Content != null)
@@ -147,5 +146,5 @@ public sealed class AlgoliaJob
147146

148147
public string IndexName { get; set; }
149148

150-
public string Content { get; set; }
149+
public string? Content { get; set; }
151150
}

backend/src/Squidex/Areas/IdentityServer/Controllers/Error/ErrorController.cs

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,20 @@
55
// All rights reserved. Licensed under the MIT license.
66
// ==========================================================================
77

8+
using System.Text.RegularExpressions;
89
using Microsoft.AspNetCore;
910
using Microsoft.AspNetCore.Diagnostics;
1011
using Microsoft.AspNetCore.Mvc;
12+
using Microsoft.Extensions.Options;
13+
using Squidex.Config;
1114
using Squidex.Infrastructure;
1215

1316
namespace Squidex.Areas.IdentityServer.Controllers.Error;
1417

15-
public sealed class ErrorController : IdentityServerController
18+
public sealed class ErrorController(IOptions<MyIdentityOptions> identityOptions) : IdentityServerController
1619
{
20+
private readonly MyIdentityOptions identityOptions = identityOptions.Value;
21+
1722
[Route("error/")]
1823
public async Task<IActionResult> Error(string? errorId = null)
1924
{
@@ -32,14 +37,12 @@ public async Task<IActionResult> Error(string? errorId = null)
3237
}
3338

3439
var source = HttpContext.Features.Get<IExceptionHandlerFeature>();
35-
3640
if (source == null)
3741
{
3842
return View("Error", vm);
3943
}
4044

4145
var exception = source.Error;
42-
4346
while (exception?.InnerException != null)
4447
{
4548
exception = exception.InnerException;
@@ -49,10 +52,42 @@ public async Task<IActionResult> Error(string? errorId = null)
4952
{
5053
vm.ErrorMessage = exception?.Message;
5154
}
55+
else if (TryGetMappedError(exception, out var mappedError))
56+
{
57+
vm.ErrorMessage = mappedError;
58+
}
5259

5360
return View("Error", vm);
5461
}
5562

63+
private bool TryGetMappedError(Exception? exception, out string message)
64+
{
65+
message = null!;
66+
67+
if (exception == null || identityOptions.OidcErrorMap == null || identityOptions.OidcErrorMap.Count == 0)
68+
{
69+
return false;
70+
}
71+
72+
foreach (var (pattern, value) in identityOptions.OidcErrorMap)
73+
{
74+
try
75+
{
76+
if (Regex.IsMatch(exception.Message, pattern))
77+
{
78+
message = value;
79+
return true;
80+
}
81+
}
82+
catch
83+
{
84+
continue;
85+
}
86+
}
87+
88+
return false;
89+
}
90+
5691
private static bool IsTestEndpoint(IExceptionHandlerFeature source)
5792
{
5893
return source.Endpoint is RouteEndpoint route && route.RoutePattern.RawText == "identity-server/test";

backend/src/Squidex/Config/Authentication/OidcHandler.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ public override Task TokenValidated(TokenValidatedContext context)
3434
return base.TokenValidated(context);
3535
}
3636

37+
public override Task AuthenticationFailed(AuthenticationFailedContext context)
38+
{
39+
return base.AuthenticationFailed(context);
40+
}
41+
3742
public override Task RedirectToIdentityProviderForSignOut(RedirectContext context)
3843
{
3944
if (!string.IsNullOrEmpty(options.OidcOnSignoutRedirectUrl))

backend/src/Squidex/Config/MyIdentityOptions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ public sealed class MyIdentityOptions
5555

5656
public string OidcResponseType { get; set; }
5757

58+
public Dictionary<string, string>? OidcErrorMap { get; set; }
59+
5860
public string? OidcOnSignoutRedirectUrl { get; set; }
5961

6062
public string[] OidcScopes { get; set; }

backend/src/Squidex/appsettings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,7 @@
651651
"oidcClient": "",
652652
"oidcSecret": "",
653653
"oidcPrompt": null,
654+
"oidcErrorMap": null,
654655
"oidcMetadataAddress": "",
655656
"oidcScopes": [
656657
"email"

0 commit comments

Comments
 (0)