Skip to content

@belgattitude/http-exception@1.1.0

Compare
Choose a tag to compare
@belgattitude belgattitude released this 21 Sep 18:07
· 365 commits to main since this release
d9f24e6

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