Skip to content

Commit

Permalink
Error map (#1185)
Browse files Browse the repository at this point in the history
* Extract mongo code to own assembly.

* Error Map.
  • Loading branch information
SebastianStehle authored Jan 27, 2025
1 parent d998f65 commit 586ca00
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public sealed class AlgoliaActionHandler(RuleEventFormatter formatter, IScriptEn
{
AppId = action.AppId,
ApiKey = action.ApiKey,
Content = serializer.Serialize(content, true),
Content = delete ? null : serializer.Serialize(content, true),
ContentId = contentId,
IndexName = indexName
};
Expand All @@ -100,7 +100,6 @@ protected override async Task<Result> ExecuteJobAsync(AlgoliaJob job,
}

var index = await clients.GetClientAsync((job.AppId, job.ApiKey, job.IndexName));

try
{
if (job.Content != null)
Expand Down Expand Up @@ -147,5 +146,5 @@ public sealed class AlgoliaJob

public string IndexName { get; set; }

public string Content { get; set; }
public string? Content { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,20 @@
// All rights reserved. Licensed under the MIT license.
// ==========================================================================

using System.Text.RegularExpressions;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Diagnostics;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using Squidex.Config;
using Squidex.Infrastructure;

namespace Squidex.Areas.IdentityServer.Controllers.Error;

public sealed class ErrorController : IdentityServerController
public sealed class ErrorController(IOptions<MyIdentityOptions> identityOptions) : IdentityServerController
{
private readonly MyIdentityOptions identityOptions = identityOptions.Value;

[Route("error/")]
public async Task<IActionResult> Error(string? errorId = null)
{
Expand All @@ -32,14 +37,12 @@ public async Task<IActionResult> Error(string? errorId = null)
}

var source = HttpContext.Features.Get<IExceptionHandlerFeature>();

if (source == null)
{
return View("Error", vm);
}

var exception = source.Error;

while (exception?.InnerException != null)
{
exception = exception.InnerException;
Expand All @@ -49,10 +52,42 @@ public async Task<IActionResult> Error(string? errorId = null)
{
vm.ErrorMessage = exception?.Message;
}
else if (TryGetMappedError(exception, out var mappedError))
{
vm.ErrorMessage = mappedError;
}

return View("Error", vm);
}

private bool TryGetMappedError(Exception? exception, out string message)
{
message = null!;

if (exception == null || identityOptions.OidcErrorMap == null || identityOptions.OidcErrorMap.Count == 0)
{
return false;
}

foreach (var (pattern, value) in identityOptions.OidcErrorMap)
{
try
{
if (Regex.IsMatch(exception.Message, pattern))
{
message = value;
return true;
}
}
catch
{
continue;
}
}

return false;
}

private static bool IsTestEndpoint(IExceptionHandlerFeature source)
{
return source.Endpoint is RouteEndpoint route && route.RoutePattern.RawText == "identity-server/test";
Expand Down
5 changes: 5 additions & 0 deletions backend/src/Squidex/Config/Authentication/OidcHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ public override Task TokenValidated(TokenValidatedContext context)
return base.TokenValidated(context);
}

public override Task AuthenticationFailed(AuthenticationFailedContext context)
{
return base.AuthenticationFailed(context);
}

public override Task RedirectToIdentityProviderForSignOut(RedirectContext context)
{
if (!string.IsNullOrEmpty(options.OidcOnSignoutRedirectUrl))
Expand Down
2 changes: 2 additions & 0 deletions backend/src/Squidex/Config/MyIdentityOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ public sealed class MyIdentityOptions

public string OidcResponseType { get; set; }

public Dictionary<string, string>? OidcErrorMap { get; set; }

public string? OidcOnSignoutRedirectUrl { get; set; }

public string[] OidcScopes { get; set; }
Expand Down
1 change: 1 addition & 0 deletions backend/src/Squidex/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,7 @@
"oidcClient": "",
"oidcSecret": "",
"oidcPrompt": null,
"oidcErrorMap": null,
"oidcMetadataAddress": "",
"oidcScopes": [
"email"
Expand Down

0 comments on commit 586ca00

Please sign in to comment.