0.7.0 • Published 4 months ago

sendable-error v0.7.0

Weekly downloads
3
License
MIT
Repository
github
Last release
4 months ago

sendable-error · GitHub license npm version

Composable errors to simplify creating useful failure responses for APIs

npm i sendable-error

Note: this package is in early development, use with caution

SendableErrors provide built-in support for:

  • An easy to use builder interface to construct errors
  • A unified way to send your errors as a JSON response
  • Error codes to easily identify error types on the client side
  • Public and private messages & details so your APIs don't leak technical information yet retaining verbose logging
  • Trace IDs allow you to identify specific errors and allows user's to point you in the right direction when they encounter a bug
  • A customizable logger interface
import { SendableError } from "sendable-error";

try {
  throw new SendableError({
    status: 400,
    code: "validation/missing-required",
    message: "Missing required field 'id'",
    public: true,
    details: {
      field: "id"
    }
  })
} catch (error) {
  return SendableError.of(error).toResponse();
}

Response with status code `400¦:

{
  "code": "validation/missing-required",
  "message": "Missing required field 'id'",
  "traceId": "8ab9c56a-90d1-5e71-b67a-d6b725837802",
  "details": {
    "field": "id"
  }
}

Getting Started

Throwing Errors

Creating a new error from scratch:

 throw new SendableError({
  code: CODE_MISSING_REQUIRED,
  message: "Missing required field 'id'",
  public: true,
  details: {
    field: "id",
  },
});

Or provide a cause for the error:

throw new SendableError({
  code: CODE_DATABASE_ERROR,
  cause: error,
});

Or even transform an error from elsewhere into a SendableError:

throw SendableError.of(error, {
  code: CODE_DATABASE_ERROR
});

Sending Errors

Express

app.use((error, req, res, next) => {
  SendableError.of(error).send(res);
});

WinterTC Compatible

export const handler = async (request: Request) => {
  try {
    // do something that might throw
  } catch (error) {
    return SendableError.of(error).toResponse();
  }
}

Others

try {
  // do something that might throw
} catch (error) {
  const responseBody = SendableError.of(error).toResponseBody();
  /* send responseBody */
}
0.7.0

4 months ago

0.6.2

4 months ago

0.6.0

9 months ago

0.5.0

11 months ago

0.5.0-alpha.3

11 months ago

0.5.0-alpha.2

11 months ago

0.5.0-alpha.1

11 months ago

0.5.0-alpha.6

11 months ago

0.5.0-alpha.5

11 months ago

0.5.0-alpha.4

11 months ago

0.4.1

12 months ago

0.4.0

12 months ago

0.4.0-alpha.4

1 year ago

0.4.0-alpha.3

1 year ago

0.4.0-alpha.2

1 year ago

0.4.0-alpha.1

1 year ago

0.3.2

1 year ago

0.3.0

2 years ago

0.3.1

2 years ago

0.2.1

2 years ago

0.2.6

2 years ago

0.2.3

2 years ago

0.2.2

2 years ago

0.2.5

2 years ago

0.2.4

2 years ago

0.1.2

2 years ago

0.2.0

2 years ago

0.1.1

2 years ago

0.1.0-alpha.16

3 years ago

0.1.0-alpha.18

3 years ago

0.1.0-alpha.17

3 years ago

0.1.0-alpha.19

3 years ago

0.1.0

3 years ago

0.1.0-alpha1

3 years ago

0.1.0-alpha2

3 years ago

0.1.0-alpha3

3 years ago

0.1.0-alpha4

3 years ago

0.1.0-alpha5

3 years ago

0.1.0-alpha6

3 years ago

0.1.0-alpha8

3 years ago

0.1.0-alpha9

3 years ago

0.1.0-alpha13

3 years ago

0.1.0-alpha15

3 years ago

0.1.0-alpha10

3 years ago

0.1.0-alpha12

3 years ago

0.1.0-alpha11

3 years ago

0.0.17

4 years ago

0.0.16

5 years ago

0.0.13

5 years ago

0.0.14

5 years ago

0.0.15

5 years ago

0.0.12

5 years ago

0.0.11

5 years ago

0.0.10

5 years ago

0.0.9

5 years ago

0.0.8

5 years ago

0.0.7

5 years ago

0.0.6

5 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago