Open Closed

UserFriendlyException from the backend causing a 403 response after providing a HTTP_ERROR_HANDLER in the frontend #3520


User avatar
0
utku.birkan created

Hi, I am trying to implement a custom 403 HTTP error page using Angular by providing an HTTP_ERROR_HANDLER function similar to the one described in the documentation. My implementation is given below.

// http-error-handler.ts
export function handleHttpErrors(injector: Injector, httpError: HttpErrorResponse) {
  if (httpError.status === 403) {
    const router = injector.get(Router);
    router.navigateByUrl('/unauthorized');
    return;
  }

  return throwError(httpError);
}

// app.module.ts
@NgModule({
  ...,
  providers: [
    ...,
    {
      provide: HTTP_ERROR_HANDLER,
      useValue: handleHttpErrors,
    },
  ]
})
export class AppModule {}

Before providing this error, UserFriendlyExceptions on the server side were handled via popups in the frontend. Now, each UserFriendlyException is captured by handleHttpErrors function as an HttpErrorResponse with status 403. We want to retain the previous 403 page behavior while using our new layout for actual, permission related 403 errors.

Relevant info:

  • ABP Framework version: v5.3.x
  • UI type: Angular
  • DB provider: EF Core
  • Tiered (MVC) or Identity Server Separated (Angular): yes

2 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    I will provide a solution soon. : )

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    You can try to custom the DefaultHttpExceptionStatusCodeFinder service to change the HttpStatusCode

    https://github.com/abpframework/abp/blob/dev/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/ExceptionHandling/DefaultHttpExceptionStatusCodeFinder.cs#L14

    https://github.com/abpframework/abp/blob/dev/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/ExceptionHandling/DefaultHttpExceptionStatusCodeFinder.cs#L72

Made with ❤️ on ABP v8.2.0-preview Updated on March 25, 2024, 15:11