Options
All
  • Public
  • Public/Protected
  • All
Menu

Namespace errors

Provide utilities for creating application errors with certain severities and optional custom information.

// esm
import { errors } from '@paychex/core';

// cjs
const { errors } = require('@paychex/core');

// iife
const { errors } = window['@paychex/core'];

// amd
require(['@paychex/core'], function({ errors }) { ... });
define(['@paychex/core'], function({ errors }) { ... });
example
import { tracker } from '../path/to/tracker';
import { fetch, createRequest } from '../path/to/datalayer';

const { rethrow, fatal } = errors;

const operation = {
base: 'my-app',
path: '/settings',
headers: {
accept: 'application/json'
}
};

export async function loadAppSettings() {
const request = createRequest(operation);
const response = await fetch(request)
.catch(rethrow(fatal({ app: 'my-app' })));
return response.data;
}

Index

Type aliases

ErrorProperties: Properties & { severity: string }
Properties: Record<string, any>
SeverityError: Error & ErrorProperties

Variables

ERROR: "ERROR" = 'ERROR'

Indicates an error that was unexpected but recoverable.

constant
example
export const tracker = trackers.create((info) => {
if (info.type === 'error') {
const error = info.data;
switch (error.severity) {
case errors.ERROR:
case errors.FATAL:
// send to error log
break;
case errors.IGNORE:
console.log(error.message);
console.log(err.stack);
break;
}
}
});
FATAL: "FATAL" = 'FATAL'

Indicates an Error that cannot be recovered from.

constant
example
export const tracker = trackers.create((info) => {
if (info.type === 'error') {
const error = info.data;
switch (error.severity) {
case errors.ERROR:
case errors.FATAL:
// send to error log
break;
case errors.IGNORE:
console.log(error.message);
console.log(err.stack);
break;
}
}
});
IGNORE: "NONE" = 'NONE'

Indicates an error that was expected and recoverable.

constant
example
export const tracker = trackers.create((info) => {
if (info.type === 'error') {
const error = info.data;
switch (error.severity) {
case errors.ERROR:
case errors.FATAL:
// send to error log
break;
case errors.IGNORE:
console.log(error.message);
console.log(err.stack);
break;
}
}
});

Functions

  • Creates a new Error instance with the optional key-value pairs mixed in. The returned Error will have the default severity of ERROR, which indicates it is unexpected but also recoverable.

    example
    import { isNil } from 'lodash';

    export function loadClientData(clientId) {
    if (isNil(clientId))
    throw errors.error('Parameter clientId is required.');
    // ...working logic here...
    }
    example
    // change error severity to FATAL
    import { isNil } from 'lodash';

    export function loadUserPermissions(userId) {
    return new Promise((resolve, reject) => {
    if (isNil(userId))
    reject(errors.error('Parameter userId is required.', errors.fatal()));
    // ...working logic here...
    });
    }

    Type parameters

    Parameters

    • message: string

      The error message.

    • data: T = null

      Optional data to assign to the Error.

    Returns SeverityError & T

    A new Error instance.

  • Returns an object literal containing the optionally specified key-value pairs along with a severity of FATAL, indicating an Error that cannot be recovered from.

    example
    import { isNil } from 'lodash';

    export function loadUserPermissions(userId) {
    if (isNil(userId))
    throw errors.error('Parameter userId is required.', errors.fatal());
    // ...working logic here...
    }
    example
    import { fetch, createRequest } from '~/path/to/data';
    import { loadClientOperation } from '../data/clients';

    const { rethrow, fatal } = errors;

    export async function loadClientData(clientId) {
    const params = { clientId };
    const request = createRequest(loadClientOperation, params);
    const response = await fetch(request).catch(rethrow(fatal(params)));
    return response.data;
    }

    Parameters

    Returns ErrorProperties

    An object map of the optionally provided key-value pairs along with a severity of FATAL.

  • Returns an object literal containing the optionally specified key-value pairs along with a severity of IGNORE, indicating an Error that was expected and can be safely ignored.

    example
    import { isNil } from 'lodash';

    const { error, ignore, rethrow } = errors;
    const cache = stores.memoryStore();

    export async function cacheResults(key, data) {
    if (isNil(key))
    throw error('Argument key not provided.', ignore());
    return await cache.set(key, data)
    .catch(rethrow(ignore({ key })));
    }

    Parameters

    Returns ErrorProperties

    An object map of the optionally provided key-value pairs along with a severity of IGNORE.

  • Mixes properties into an Error instance to assist with triage and debugging.

    NOTE: This method expects 2 arguments (an Error and an object literal) and is curried. That means you can provide the arguments at any time. They can also be provided in any order. These are all the same:

    rethrow(e, { params });
    rethrow({ params }, e);
    rethrow(e)({ params });
    rethrow({ params })(e);
    param}

    props Properties to mix into the Error instance.

    throws

    An Error with the properties mixed in.

    example
    somePromiseMethod(params)
    .then(handleResult)
    .catch(errors.rethrow({ params }));
    example
    try {
    someMethod(params);
    } catch (e) {
    errors.rethrow(e, { params });
    }
    example
    import { fetch, createRequest } from '~/path/to/data';
    import { loadClientOperation } from '../data/clients';

    const { rethrow, fatal } = errors;

    export async function loadClientData(clientId) {
    const params = { clientId };
    const request = createRequest(loadClientOperation, params);
    const response = await fetch(request).catch(rethrow(fatal(params)));
    return response.data;
    }

    Returns CurriedFunction2<Error | Properties, Error | Properties, never>

  • Mixes properties into an Error instance to assist with triage and debugging.

    NOTE: This method expects 2 arguments (an Error and an object literal) and is curried. That means you can provide the arguments at any time. They can also be provided in any order. These are all the same:

    rethrow(e, { params });
    rethrow({ params }, e);
    rethrow(e)({ params });
    rethrow({ params })(e);
    param}

    props Properties to mix into the Error instance.

    throws

    An Error with the properties mixed in.

    example
    somePromiseMethod(params)
    .then(handleResult)
    .catch(errors.rethrow({ params }));
    example
    try {
    someMethod(params);
    } catch (e) {
    errors.rethrow(e, { params });
    }
    example
    import { fetch, createRequest } from '~/path/to/data';
    import { loadClientOperation } from '../data/clients';

    const { rethrow, fatal } = errors;

    export async function loadClientData(clientId) {
    const params = { clientId };
    const request = createRequest(loadClientOperation, params);
    const response = await fetch(request).catch(rethrow(fatal(params)));
    return response.data;
    }

    Parameters

    Returns CurriedFunction1<Error | Properties, never>

  • Mixes properties into an Error instance to assist with triage and debugging.

    NOTE: This method expects 2 arguments (an Error and an object literal) and is curried. That means you can provide the arguments at any time. They can also be provided in any order. These are all the same:

    rethrow(e, { params });
    rethrow({ params }, e);
    rethrow(e)({ params });
    rethrow({ params })(e);
    param}

    props Properties to mix into the Error instance.

    throws

    An Error with the properties mixed in.

    example
    somePromiseMethod(params)
    .then(handleResult)
    .catch(errors.rethrow({ params }));
    example
    try {
    someMethod(params);
    } catch (e) {
    errors.rethrow(e, { params });
    }
    example
    import { fetch, createRequest } from '~/path/to/data';
    import { loadClientOperation } from '../data/clients';

    const { rethrow, fatal } = errors;

    export async function loadClientData(clientId) {
    const params = { clientId };
    const request = createRequest(loadClientOperation, params);
    const response = await fetch(request).catch(rethrow(fatal(params)));
    return response.data;
    }

    Parameters

    Returns CurriedFunction1<Error | Properties, never>

  • Mixes properties into an Error instance to assist with triage and debugging.

    NOTE: This method expects 2 arguments (an Error and an object literal) and is curried. That means you can provide the arguments at any time. They can also be provided in any order. These are all the same:

    rethrow(e, { params });
    rethrow({ params }, e);
    rethrow(e)({ params });
    rethrow({ params })(e);
    param}

    props Properties to mix into the Error instance.

    throws

    An Error with the properties mixed in.

    example
    somePromiseMethod(params)
    .then(handleResult)
    .catch(errors.rethrow({ params }));
    example
    try {
    someMethod(params);
    } catch (e) {
    errors.rethrow(e, { params });
    }
    example
    import { fetch, createRequest } from '~/path/to/data';
    import { loadClientOperation } from '../data/clients';

    const { rethrow, fatal } = errors;

    export async function loadClientData(clientId) {
    const params = { clientId };
    const request = createRequest(loadClientOperation, params);
    const response = await fetch(request).catch(rethrow(fatal(params)));
    return response.data;
    }

    Parameters

    Returns never