create-error-types
Advanced tools
Comparing version 2.0.1 to 3.0.0
@@ -13,3 +13,3 @@ import normalizeException from"normalize-exception"; | ||
export const callErrorHandler=function( | ||
{CustomErrorTypes,InternalError,bugsUrl}, | ||
{CustomErrorTypes,UnknownError,bugsUrl}, | ||
error) | ||
@@ -20,3 +20,3 @@ { | ||
errorA: | ||
new InternalError(getInternalErrorMessage(bugsUrl),{cause:errorA}); | ||
new UnknownError(getUnknownErrorMessage(bugsUrl),{cause:errorA}); | ||
}; | ||
@@ -32,5 +32,5 @@ | ||
const getInternalErrorMessage=function(bugsUrl){ | ||
const getUnknownErrorMessage=function(bugsUrl){ | ||
return bugsUrl===undefined?"":`Please report this bug at: ${bugsUrl}`; | ||
}; | ||
//# sourceMappingURL=handler.js.map |
@@ -11,3 +11,3 @@ import { ErrorName, OnCreate, CustomError, ErrorParams } from 'error-type' | ||
/** | ||
* URL where users should report internal errors/bugs. | ||
* URL where users should report unknown errors. | ||
* | ||
@@ -72,3 +72,3 @@ * @example 'https://github.com/my-name/my-project/issues' | ||
*/ | ||
errorHandler: ErrorHandler<ErrorNames> | ||
errorHandler: ErrorHandler<ErrorNames, ErrorParamsArg> | ||
} | ||
@@ -79,5 +79,6 @@ | ||
*/ | ||
export type ErrorHandler<ErrorNames extends ErrorName = ErrorName> = ( | ||
error: unknown, | ||
) => CustomError<ErrorNames> | ||
export type ErrorHandler< | ||
ErrorNames extends ErrorName = ErrorName, | ||
ErrorParamsArg extends ErrorParams = ErrorParams, | ||
> = (error: unknown) => CustomError<ErrorNames | 'UnknownError', ErrorParamsArg> | ||
@@ -84,0 +85,0 @@ export type { ErrorName, CustomError, ErrorParams } |
import{callErrorHandler}from"./handler.js"; | ||
import{getOpts}from"./opts.js"; | ||
import{getCustomErrorTypes,createInternalError}from"./types.js"; | ||
import{getCustomErrorTypes,createUnknownError}from"./types.js"; | ||
@@ -10,6 +10,6 @@ | ||
const CustomErrorTypes=getCustomErrorTypes(errorNames,onCreate); | ||
const InternalError=createInternalError(); | ||
const UnknownError=createUnknownError(); | ||
const errorHandler=callErrorHandler.bind(undefined,{ | ||
CustomErrorTypes, | ||
InternalError, | ||
UnknownError, | ||
bugsUrl}); | ||
@@ -16,0 +16,0 @@ |
@@ -17,5 +17,5 @@ import errorType from"error-type"; | ||
const validateErrorName=function(errorName){ | ||
if(errorName===INTERNAL_ERROR_NAME){ | ||
throw new Error(`Error name must not be "${INTERNAL_ERROR_NAME}". | ||
"${INTERNAL_ERROR_NAME}" is reserved for exceptions matching none of the error types.`); | ||
if(errorName===UNKNOWN_ERROR_NAME){ | ||
throw new Error(`Error name must not be "${UNKNOWN_ERROR_NAME}". | ||
"${UNKNOWN_ERROR_NAME}" is reserved for exceptions matching none of the error types.`); | ||
} | ||
@@ -55,7 +55,7 @@ }; | ||
export const createInternalError=function(){ | ||
return errorType(INTERNAL_ERROR_NAME); | ||
export const createUnknownError=function(){ | ||
return errorType(UNKNOWN_ERROR_NAME); | ||
}; | ||
const INTERNAL_ERROR_NAME="InternalError"; | ||
const UNKNOWN_ERROR_NAME="UnknownError"; | ||
//# sourceMappingURL=types.js.map |
{ | ||
"name": "create-error-types", | ||
"version": "2.0.1", | ||
"version": "3.0.0", | ||
"type": "module", | ||
@@ -5,0 +5,0 @@ "exports": "./build/src/main.js", |
@@ -12,5 +12,4 @@ [![Codecov](https://img.shields.io/codecov/c/github/ehmicky/create-error-types.svg?label=tested&logo=codecov)](https://codecov.io/gh/ehmicky/create-error-types) | ||
- Create [custom error types](#create-custom-error-types) | ||
- Automatically separate (unhandled) [internal errors](#internal-errors) from | ||
(handled) user errors | ||
- Internal errors indicate where to [report bugs](#bug-reports) | ||
- Automatically separate known and [unknown errors](#unknown-errors) | ||
- Unknown errors indicate where to [report bugs](#bug-reports) | ||
- Set properties on [individual errors](#set-error-properties), or on | ||
@@ -99,3 +98,3 @@ [all errors of the same type](#error-type-properties) | ||
URL where users should [report internal errors/bugs](#bug-reports). | ||
URL where users should [report unknown errors](#bug-reports). | ||
@@ -166,3 +165,3 @@ #### onCreate | ||
// ... | ||
} else if (error.name === 'InternalError') { | ||
} else if (error.name === 'UnknownError') { | ||
// ... | ||
@@ -186,12 +185,12 @@ } | ||
### Internal errors | ||
### Unknown errors | ||
Internal errors/bugs can be distinguished from user errors by: | ||
All errors should use _known types_: the ones returned by | ||
[`createErrorTypes()`](#create-custom-error-types). Errors with an _unknown | ||
type_ should be handled in `try {} catch {}` and [re-thrown](#set-error-type) | ||
with a _known type_ instead. | ||
- Handling any possible errors in `try {} catch {}` | ||
- Re-throwing them [with a known error type](#set-error-type) | ||
The [`errorHandler()`](#error-handler) assigns the `UnknownError` type to any | ||
error with an _unknown type_. | ||
The [`errorHandler()`](#error-handler) assigns the `InternalError` type to any | ||
error with an unknown type. | ||
<!-- eslint-disable unicorn/no-null --> | ||
@@ -204,3 +203,3 @@ | ||
getUserId(null) // InternalError: Cannot read properties of null (reading 'id') | ||
getUserId(null) // UnknownError: Cannot read properties of null (reading 'id') | ||
``` | ||
@@ -216,3 +215,3 @@ | ||
any [internal error](#internal-errors) will include the following message. | ||
any [unknown error](#unknown-errors) will include the following message. | ||
@@ -219,0 +218,0 @@ ``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
28228
188
356