r37r0m0d3l/of

View on GitHub
src/core/ofError.js

Summary

Maintainability
A
0 mins
Test Coverage
import { ERR_UNKNOWN } from "../const/error.js";

/**
 * @name ofError
 * @param {Promise} promise
 * @param {*=} overrideError
 * @returns {Promise<*>}
 */
export function ofError(promise, overrideError) {
  return Promise.resolve(promise)
    .then(() => undefined)
    .catch((caughtError) => {
      if (caughtError === undefined || caughtError === null) {
        caughtError = new Error(ERR_UNKNOWN);
      }
      if (overrideError instanceof Error) {
        caughtError = overrideError;
      } else if (typeof overrideError === "string") {
        caughtError.message = overrideError;
      }
      return caughtError;
    });
}