Skip to content

Component/declarative error handling (ala React 16's <ErrorBoundary/>) #18509

Closed

Description

@craigsmitham

I'm submitting a...


[ ] Regression (a behavior that used to work and stopped working in a new release)
[ ] Bug report  
[x] Feature request
[x] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

React recently introduced a concept called "Error Boundaries" (https://facebook.github.io/react/blog/2017/07/26/error-handling-in-react-16.html). Basically you wrap key areas of your app with .... and any component/template errors within can be handled at that level. I've run into issues with my Angular 4 app where an error in a component will cause the app to break ... sometimes the app is still navigable but often times rendering is messed up and the app just needs a restart. Most blog posts (and the Angular documentation) on error handling don't deal with rendering issues, just logging/handling any exceptions that may occur. What are strategies for gracefully handling "breaking errors" in an Angular app? Would something like make sense for Angular?

Edit: after discussing with @robwormald, it seems the current proscribed solution for handling uncaught errors would be to log them and then reload the entire app via the ErrorHandler mechanism already in Angular. This is still less than ideal (although ideally there would be no errors :-)). As applications grow, and as third party components are added/upgraded - it would be useful to introduce an the concept of an ErrorBoundary for two primary reasons:

  1. allows more graceful and selective degradation of the app (if a child components underneath an ErrorBoundary fail, they can be restarted)
  2. Error boundaries will introduce 'bulkheads' that ensure a single error won't sink the entire ship (forcing an app reload/restart), which will be more important the more likely errors are to occur (size of app, # external components, etc)

Activity

  1. added
    area: core
    feature
    on Aug 3, 2017
  2. added this to the Backlog milestone on Jan 23, 2018
  3. haskelcurry commented on May 23, 2018

    @haskelcurry

    This would be very helpful. The fact that in Angular entire app "sinks" on a single error in some component is just embarrassing 👎

  4. itsdevdom commented on Oct 23, 2018

    @itsdevdom

    Completely agree! Having error handlers on a component level would be very helpful, allowing for more granular and flexible error management.

    In our case, we have a (let's call it) "building block" in our UI library that consists of multiple components, directives and services. Our idea was to have an event on the outermost component that emits once an errors gets thrown - no matter at what place down the component hierarchy it occurred.

    As a workaround, we've implemented a service to at least allow this functionality for errors we throw ourselves. Nonetheless, if Angular fails at some point, we have no ability to catch it. The application would be responsible now, and it basically must redirect to an error page / reload the application - because it cannot handle this error better (e.g. by just not rendering that part of the page).

  5. haskelcurry commented on Jan 22, 2019

    @haskelcurry

    Any updates on this?

  6. pauldraper commented on Jun 6, 2019

    @pauldraper
    Contributor

    error-boundary.component.ts

    import {
      ChangeDetectorRef,
      Component,
      ErrorHandler,
      ContentChild,
      TemplateRef,
    } from '@angular/core';
    
    @Component({
      selector: 'error-boundary',
      templateUrl: './error-boundary.component.html',
    })
    export class ErrorBoundaryComponent {
      constructor(
        private readonly changeDetector: ChangeDetectorRef,
        private readonly errorHandler: ErrorHandler,
      ) {
        this.changeDetector.detach();
      }
    
      @ContentChild(TemplateRef)
      template: TemplateRef<{}>;
    
      ngDoCheck() {
        try {
          this.changeDetector.detectChanges();
        } catch (e) {
          this.errorHandler.handleError(e);
        }
      }
    }

    error-boundary.component.html

    <ng-container *ngTemplateOutlet="template"></ng-container>

    example.component.html

    <header>Header<header>
    <error-boundary>{{ error() }}</error-boundary>
    <footer>Footer</footer>
  7. sandrooco commented on Jan 19, 2021

    @sandrooco

    @pauldraper I'm trying to implement this with a child component from a library (into which I obviously can't build error handling). Do you have an idea how I could achieve this?

  8. haskelcurry commented on Jan 19, 2021

    @haskelcurry

    @sandrooco obviously, there is no way. This is not a problem that can be easily solved by some component. It's a global serious problem of Angular framework, which is being consistently ignored by the Angular Team.

  9. shivekundefined commented on Apr 13, 2021

    @shivekundefined

    There is one npm package that can help us to achieve error handling on component just like React does.
    https://www.npmjs.com/package/ngx-error-boundary
    This should be helpful

  10. haskelcurry commented on Apr 13, 2021

    @haskelcurry

    @shivekundefined No, that's a totally different thing. I checked the code, and this library basically just provides a service with a "handleError" method + a component to do something with an error.
    @pauldraper 's answer above is much shorter and useful.
    BUT, we're not talking about mere error handling here. We're talking about low-level Angular functionality: ability to ignore the errors while rendering the components, and continue to run the app gracefully. (remember that you have 3rd party components from OTHER libs, too).
    That's why it says Error Boundaries: the error has power only in limited boundaries. We're not saying "Error Handling" 🙂

  11. added
    feature: under consideration
    on Jun 4, 2021
  12. crutchcorn commented on Sep 28, 2023

    @crutchcorn
    Contributor

    I believe I have an API that would solve this issue:

    #51941

    Would love to hear your thoughts/thumbs up/down on the matter. Even wrote a blog post (linked in issue) why it's needed

  13. PrenexNormalForm commented on Oct 16, 2023

    @PrenexNormalForm

    This would be extremely valuable to my team as currently there is no good way to have distinct areas of the page fail gracefully without interfering with rendering the rest of the page when there is an error raised in the template.

  14. bredziniak commented on May 25, 2024

    @bredziniak

    This would be great, it could also be achieved by new control syntax, something like:
    @Try {

    } @catch(e) {
    <error-card [error]="e" />
    }

  15. hv0905 commented on Dec 4, 2024

    @hv0905

    Hi, I’m looking forward to seeing this feature. My application has several components that process user-provided data, and handling errors within them is complex. I’m hoping for a way to manage errors outside of the components.

  16. menosprezzi commented on May 15, 2025

    @menosprezzi

    I was thinking how it play alongside the @defer block syntax since it has the @error block. I think it will be misunderstood. I've always been in favor that @error should be called @catch, even though it cant catch errors throwed by the component, but aiming to support features like this as soon as possible (and following the coherence of the other blocks like @if and @for), without generating confusion with a functionality like this, converging on a single taxonomy.

    If it does not converge, the markup of a deferrable view with propper error handling will be something like this 😵‍💫:

    @defer {
        @try {
    	<error-throwing />
        } @catch {
    	<handle-error [error]="$error.message"/>
        } 
    }
    @loading {
        <loading />
    }
    @error {
        <handle-error [error]="$error.message"/>
    }

    Or even worse:

    @try {
        @defer {
    	<error-throwing />
        }
        @loading {
            <loading />
        }
        @error {
            <handle-error [error]="$error.message"/>
        }
    } @catch {
        <handle-error [error]="$error.message"/>
    } 

    I think that the catch block functionality should be embedded to @defer (and deprecate the @error in the syntax - since it was not even should exist before, btw 🙃):

    @defer {
       <error-throwing />
    }
    @loading {
        <loading />
    }
    @catch {
        <handle-error [error]="$error.message"/>
    }
  17. raven-dever commented on Oct 23, 2025

    @raven-dever

    +1 on this. I think it's a quite needed feature to ensure the stability of Angular apps

  18. SSE-programmer commented on Mar 18, 2026

    @SSE-programmer

    I agree. We need a solution to this.

  19. chriswoodie commented on Jun 8, 2026

    @chriswoodie

    The new @boundary/@error syntax solves this, but will it work for images whose src fails to load to allow us to provide an error image instead?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: corecore: error handlingfeaturefeature: under consideration

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions