0.0.11 • Published 2 years ago

contextual-error v0.0.11

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

contextual-error

Emit errors described by a code and context.

Installation

yarn add contextual-error
# Or if you are using NPM:
# npm install --save contextual-error

Example

import { ContextualError } from 'contextual-error';

type AppErrorContext = (
  {
    code: 'ServerError';
    metadata: Record<string, never>;
  }
  | {
    code: 'ValidationError';
    metadata: {
      name: string;
      reason: string;
      value?: unknown;
    };
   }
);

class AppError extends ContextualError<AppErrorContext> {}

try {

  const request = {
    age: 'abc',
  };

  if ('number' !== typeof request.age) {
    throw new AppError({
      code: 'ValidationError',
      metadata: {
        name: 'age',
        reason: 'Value must be a number',
        value: request.age,
      },
    });
  }

} catch (err) {

  console.log(err);
  // AppError: ValidationError
  //   ...stack...
  //   context: {
  //     code: 'ValidationError',
  //     metadata: {
  //       name: 'age',
  //       reason: 'Value must be a number',
  //       value: 'abc',
  //     },
  //   },
  //   timestamp: '2000-01-01T00:00:00Z'

  if (err instanceof AppError) {
    switch (err.context.code) {
      case 'ValidationError':
        const { name, reason, value } = err.context.metadata;
        alert(`Invalid value provided for "${name}". ${reason} but you provided "${String(value ?? '')}".`);
        // Invalid value provided for "age". Value must be a number but you provided "abc".
        break;
      case 'ServerError':
        alert('Please try again later.');
        break;
    }
  }

}
0.0.11

2 years ago

0.0.10

2 years ago

0.0.9

2 years ago

0.0.8

2 years ago

0.0.7

2 years ago

0.0.6

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago