forked from Speedup-lib/nodejs-standard-error-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
9 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,11 @@ | ||
|
||
/** | ||
* Error code type | ||
*/ | ||
export type ErrorCodeType = string | number; | ||
|
||
export type ErrorOptions = { | ||
code?: ErrorCodeType | ||
} | ||
/** | ||
* Error code | ||
*/ | ||
code?: ErrorCodeType; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,4 @@ | ||
/** | ||
* Utility functions | ||
*/ | ||
|
||
import { ErrorCodeType, ErrorParameters } from './error_base'; | ||
|
||
/** | ||
* Get either default or predefined error message | ||
* @param code Error code or message (e.g. E_NOT_FOUND) | ||
* @param config Configuration object | ||
*/ | ||
export const getEitherDefaultOrPredefinedErrorMessage = ( | ||
opts: ErrorParameters, | ||
): string => { | ||
// set default error message | ||
let errorMessage = getDefaultErrorMessage(opts.code); | ||
|
||
// error message is inherited from the error message | ||
if (config && config.error) { | ||
errorMessage = config.error.message; | ||
} | ||
|
||
// error message is overwritten by the user | ||
if (config && config.message) { | ||
errorMessage = config.message; | ||
} | ||
|
||
return errorMessage; | ||
}; | ||
|
||
export const isNull = (inp: any) => inp === null; | ||
export const isUndefined = (inp: any) => typeof inp === 'undefined'; | ||
export const isNullOrUndefined = (inp: any) => isNull(inp) || isUndefined(inp); | ||
|
||
export const isString = (inp: any) => typeof inp === 'string'; | ||
|
||
export const isNonEmptyString = (inp: any) => | ||
isString(inp) === true && inp.length > 0; | ||
|
||
export const getErrorMessage = ( | ||
message: string | undefined, | ||
code: ErrorCodeType, | ||
) => | ||
isNonEmptyString(message) === true | ||
? message! | ||
: `Error '${code}' was occurred.`; |