Skip to content

Commit

Permalink
Minor updates.
Browse files Browse the repository at this point in the history
  • Loading branch information
DManavi committed May 25, 2022
1 parent cbb4850 commit b5e5697
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 45 deletions.
11 changes: 8 additions & 3 deletions src/types.ts
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;
};
43 changes: 1 addition & 42 deletions src/util.ts
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.`;

0 comments on commit b5e5697

Please sign in to comment.