React-router errorElement not catching errors on child routes (from within a function)

I am using React router dom version 6.25.1.

My router is defined as

const router = [
  {
    element: <LayoutThatReturnsOutlet/>,
    loader: myLoader,
    errorElement: <ErrorElement />,
    children: [
      { path: topLevel, element: <TopLevelDashboard /> },
      { path: route1, element: <Element1 /> },
      { path: route2, element: <Element2 /> },
    ],
  },
]

And is used here as follows:

<RouterProvider router={createBrowserRouter(router)} />

But when I try to get the ErrorElement to be returned by doing a simple

throw new Error("Sample error")

in the TopLevelDashboard element, nothing happens (it logs the error to the console but doesn’t show the error element or crash the app – i.e. it just throws the error silently). I have added a path on the top level also in case that was missing, but still no luck (for reference the app’s base URL has /someString/someOtherString format, so running locally looks like localhost:port/someString/someOtherString when on the root route).

I’ve read from the docs that errors ‘bubble up’ to the top level but I’m not sure why they wouldn’t be caught in the error element since there is an outlet on the default template.

The error element does work at the top level (for example if an error is thrown from the loader) but nowhere else.

Trying to define error elements on individual routes is also not working for me, though I’d prefer to have one global error handler.

Is there something obvious I’m missing?

EDIT: After some tinkering I have discovered that the ErrorElement does work, but only when the error is thrown as soon as the element is rendered.

I am trying to throw an error from within a function (i.e. when error condition is true, throw new Error(“etc”)), which is where the errorElement is not catching it

1

I am trying to throw an error from within a function (i.e. when error
condition is true, throw new Error(“etc”)), which is where the
errorElement is not catching it

Read the errorElement description a bit more closely (emphasis mine):

When exceptions are thrown in loaders, actions, or component
rendering
, instead of the normal render path for your Routes
(<Route element>), the error path will be rendered (<Route errorElement>) and the error made available with useRouteError.

and Route errorElement/ErrorBoundary

When a route throws an exception while rendering, in a loader or in
an action, this React Element/Component will render instead of the
normal element/Component.

In other words, the errorElement is used when there is an exception in the rendering of the routed component, or in any of the route loaders/actions.

Any exceptions thrown in callbacks and handlers should be handled normally, e.g. in a try/catch, a Promise chain’s .catch blocks, or a “failure”/”onFailure” callback. This means if you have any code in a useEffect hook callback or a UI event handler like onClick that potentially throws exceptions, the exceptions should be caught and handled in the component code.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật