Merging stacktrace of inner exceptions

Extend Error constructor argument for merging stacktrace.

try {
  // ...
} catch (err) {
  throw new Error("Custom Error Text", err);

Some custom error packages, e.g. extensible-custom-error and trace-error, support for holding inner exceptions and emitting it in stacktrace.

This idea depends on Stage 1 Error Stacks.

See also:

Can you describe the problem you are trying to solve or the use case you see more?

It is especially useful for clean architecture. When we make exceptions per layer, we can chain them keeping original information.

This concept is already well-known among server-side engineers. As an example, let's show the implementation of PHP in the Laravel Framework as JavaScript code.

class UserRepository extends Repository {
  find(id) {
    // return User or null

  findOrFail(id) {
    const user = this.find(id);
    if (!user) {
      throw new ModelNotFoundException(`User [${id}} not found.`);
    return user;
class ExceptionHandler
  render(e) {
    if (e instanceof AuthenticationException) {
      return new HttpException(401, e);
    if (e instanceof AuthorizationException) {
      return new HttpException(403, e);
    if (e instanceof ModelNotFoundException) {
      return new HttpException(404, e);
    return new HttpException(500, e);

ModelNotFoundException indicates that the Model corresponding to ID was not found, but it does not have any HTTP status information. ExceptionHandler wrap it with HttpException to append HTTP 404 status information.

1 Like

The Error Cause proposal seems to solve this issue. Thanks!

1 Like

A additional feature is needed to merge internal errors into the stack trace.