Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@belgattitude/http-exception

belgattitude1.6kMIT1.5.0TypeScript support: included

Warning: has been moved to @httpx/exception. Please update.

http-error, http-exception, error, exception, node, browser, typescript

readme

@belgattitude/http-exception

Warning ⚠️ This package have been moved to https://github.com/belgattitude/httpx ⚠️

No breaking changes, just update from @belgattitude/http-exception to @httpx/exception.

Delightful http exceptions. Crafted with node, browsers, ssr frameworks and error logging in mind.

npm size node browserslist dist ci codecov techdebt maintainability license

Highlights

Documentation

👉 See full documentation on https://belgattitude.github.io/http-exception. 👈

Install

npm install @belgattitude/http-exception  # via npm
yarn add @belgattitude/http-exception     # via yarn

Quick start

Simple named exceptions:

import {
  HttpGatewayTimeout,
  HttpInternalServerError,
  HttpNotFound,
  HttpServiceUnavailable,
} from "@belgattitude/http-exception";

throw new HttpNotFound(); // message = 'Not found', statusCode = 404

// Custom message
throw new HttpServiceUnavailable("Service temporarily unavailable");

// Custom context
throw new HttpInternalServerError({
  message: "Oups, this is on our side.",
  url: "https://api.dev/gateway",
  code: "EXTERNAL_SERVICE_TIMEOUT",
  cause: new HttpGatewayTimeout({
    code: "This Serverless Function has timed out",
    errorId: "cdg1::h99k2-1664884491087-b41a2832f559",
  }),
});

By status code

import { createHttpException } from "@belgattitude/http-exception";

const e404 = createHttpException(404);
const e500 = createHttpException(500, { message: "Server error" });

Serialization

import { fromJson, toJson } from "@belgattitude/http-exception/serializer";

const e = new HttpForbidden();

const json = toJson(e);
const deserialized = fromJson(json);

More in the docs: https://belgattitude.github.io/http-exception

Support

Don't hesitate and open an issue.

Contributors

Contributors are warmly welcomed. Have a look to the CONTRIBUTING document.

Sponsors ♥

If you are enjoying some of my OSS guides or libs for your company, I'd really appreciate a sponsorship, a coffee or a dropped star. That gives me a tasty morning boost and help me to make some of my ideas come true 🙏

License

MIT

changelog

@belgattitude/http-exception

1.5.0

Minor Changes

  • #204 c128653 Thanks @belgattitude! - Package moved to @httpx/exception.

    ⚠️ Version 1.5.0 will be the last one ! We've moved to https://github.com/belgattitude/httpx ⚠️

    No breaking changes, just update from @belgattitude/http-exception to @httpx/exception.

    The change to httpx namespace makes it shorter, allow more packages to be grouped and eventually will be managed inside a github org.

1.4.0

Minor Changes

1.3.4

Patch Changes

1.3.3

Patch Changes

1.3.2

Patch Changes

1.3.1

Patch Changes

1.3.0

Minor Changes

  • #75 89d2dd8 Thanks @belgattitude! - Added method, code and errorId params.

    | Name | Type | Description | | ------- | --------- | --------------------------------------------- | | url | string? | url on which the error happened | | method | string? | http method used to load the url | | code | string? | Custom code (ie: 'AbortError', 'E-1234'...) | | errorId | string? | Unique error identifier (ie: uuid, nanoid...) |

    const err = new HttpRequestTimeout({
      url: 'https://api.dev/user/belgattitude',
      method: 'GET',
      code: 'NETWORK_FAILURE',
      errorId: nanoid(), // can be shared by frontend/backend
    });
    console.log(err.url, err.method, err.code, err.errorId);

Patch Changes

1.2.0

Minor Changes

  • #67 7208e7b Thanks @belgattitude! - Export convertToSerializable and createFromSerializable

    import {
      convertToSerializable,
      createFromSerializable,
    } from '@belgattitude/http-exception/serializer';
    
    const serializableObject = convertToSerializable(new HttpForbidden());
    const exception = createFromSerializable(serializableObject);

Patch Changes

1.1.0

Minor Changes

  • #33 67be0fb Thanks @belgattitude! - Add HttpException json serializer.

    Two new methods fromJson and toJson exported from @belgattitude/http-exception/serializer.

    HttpException can be serialized to json and vice-versa. It can be useful in ssr frameworks such as nextjs whenever a server error should be shared within the browser context (see also the excellent superjson).

    Serialization supports the Error.cause but totally ignores it the runtime (node or browser) does not support it (or without polyfills).

    Additionally, you can pass any native errors (Error, EvalError, RangeError, ReferenceError, SyntaxError, TypeError, URIError) as well as a custom one (the later will be transformed to the base type Error). That was necessary to support the cause param.

    | Method | | ------------------------------------------------------------------- | | toJson(HttpException | NativeError | Error): string | | fromJson(string): HttpException | NativeError | Error |

    import {
      HttpForbidden,
      HttpUnavailableForLegalReasons,
    } from '@belgattitude/http-exception';
    import { fromJson, toJson } from '@belgattitude/http-exception/serializer';
    
    const e = new HttpForbidden({
      url: 'https://www.cool.me',
      /*
        cause: new HttpUnavailableForLegalReasons({
            cause: new Error('example with cause')
        }),
         */
    });
    
    const json = toJson(e);
    const exception = fromJson(json); // e === exception

1.0.2

Patch Changes

  • #51 421b36d Thanks @belgattitude! - Fix Error.cause on node < 16.9 and browsers that don't support for it.

    • Browser currently 89% support: caniuse#error.cause - (89% supports it as of sept 2022)
    • Node from 16.9.0 as per mdn.

    The strategy used can be summarized as:

    If the browser or the node runtime does not support Error.cause parameter in the constructor, it will simply be discarded. ie:

    const err = new HttpNotFound({cause: new Error()});
    console.log(err.cause) -> undefined if no support
    console.log(err.cause) -> Error cause if supported

    To enable older browser or previous node versions, there's 2 polyfills that should do the job

1.0.1

Patch Changes

1.0.0

Major Changes

Patch Changes

0.1.10-canary.1

Patch Changes

0.1.10-canary.0

Patch Changes

0.1.9

Patch Changes

0.1.9-canary.1

Patch Changes

0.1.9-canary.0

Patch Changes

0.1.8

Patch Changes

0.1.7

Patch Changes

0.1.6

Patch Changes

0.1.5

Patch Changes

0.1.4

Patch Changes

0.1.3

Patch Changes

0.1.2

Patch Changes

0.1.1

Patch Changes