-
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.
Merge pull request #3 from dancastillo/v0.0.2-esm-dc-error
feat: v0.0.2 esm and dc error
- Loading branch information
Showing
10 changed files
with
153 additions
and
121 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -137,3 +137,6 @@ package-lock.json | |
|
||
# yarn dependencies | ||
yarn.lock | ||
|
||
# notes | ||
notes.txt |
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# @dancastillo/error | ||
|
||
## 0.0.2 | ||
|
||
### Patch Changes | ||
|
||
- Update to esm and update to return dc error object | ||
- 857869e: Initial Release |
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
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,76 +1,27 @@ | ||
import { format } from 'node:util' | ||
import { transformCodeToErrorCode, transfromErrorCodeToErrorName, transfromErrorCodeToHttpStatusCode } from './helper' | ||
import { ErrorCode } from './types' | ||
import { DCError } from './types/dc-error.js' | ||
|
||
/** | ||
* Create error | ||
* | ||
* @param {ErrorCode | string} code - The error code of commonly thrown errors | ||
* @param {string} message - The error message | ||
* @param {string[]} details - Additional details | ||
* @param {number} statusCode - HTTP Status Code | ||
* @returns {Error} - The error object | ||
* Create Custom Error | ||
*/ | ||
export function createError(code: ErrorCode | string, message: string, details: string[] = [], statusCode?: number) { | ||
export function createError<T = unknown>( | ||
code: number | string, | ||
message: string, | ||
details: string[] = [], | ||
meta?: T | ||
): DCError<T> { | ||
if (!code) throw new Error('Error code must not be empty') | ||
if (!message?.trim()) throw new Error('Error message must not be empty') | ||
|
||
const errCode = transformCodeToErrorCode(code.toUpperCase()) | ||
const errName = transfromErrorCodeToErrorName(errCode) | ||
const errStatusCode = statusCode ?? transfromErrorCodeToHttpStatusCode(errCode) | ||
const errMessage = message.trim() | ||
|
||
/** | ||
* Return error as string | ||
* | ||
* @param {string} formattedErrMessage - The formatted error message | ||
* @returns {string} | ||
*/ | ||
function toString(formattedErrMessage: string): string { | ||
return `${errName} [${errCode}]: ${formattedErrMessage}` | ||
} | ||
|
||
/** | ||
* Base Error Class | ||
*/ | ||
class BaseError extends Error { | ||
name: string | ||
message: string | ||
details: string[] | ||
code: string | ||
statusCode: number | ||
|
||
/** | ||
* Constructor | ||
* | ||
* @param {string} args.name - error name | ||
* @param {string} args.message - error message | ||
* @param {string[]} args.details - additional details | ||
* @param {number} args.statusCode - optional parameter HTTP Status Code | ||
*/ | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
constructor(...args: any[]) { | ||
super(...args) | ||
this.name = errName | ||
this.message = format(errMessage, ...args) | ||
this.details = details | ||
this.code = errCode | ||
this.statusCode = errStatusCode | ||
|
||
// Ensuring stack trace is captured | ||
if (Error.captureStackTrace) { | ||
Error.captureStackTrace(this, BaseError) | ||
} | ||
} | ||
|
||
/** | ||
* Return error as string | ||
* @returns {string} | ||
*/ | ||
toString(): string { | ||
return toString(this.message) | ||
} | ||
if (details && !Array.isArray(details)) throw new Error('Error details must be an array') | ||
|
||
details.forEach((detail) => { | ||
if (typeof detail !== 'string') throw new Error('Error details must contain string values') | ||
if (!detail?.trim()) throw new Error('Error details must not contain empty string values') | ||
}) | ||
|
||
return { | ||
message: message, | ||
details: details, | ||
code: code, | ||
meta: meta, | ||
} | ||
|
||
return BaseError | ||
} |
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
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,2 +1,11 @@ | ||
export { createError } from './create-error' | ||
export { ErrorCode } from './types/index' | ||
export { createError } from './create-error.js' | ||
export { type DCError } from './types/dc-error.js' | ||
export { ErrorCode } from './types/http.types.js' | ||
export { | ||
CODE_STRING_TO_ERROR_CODE_MAP, | ||
transformCodeToErrorCode, | ||
ERROR_CODE_TO_ERROR_NAME_MAP, | ||
transfromErrorCodeToErrorName, | ||
ERROR_CODE_TO_HTTP_STATUS_CODE_MAP, | ||
transfromErrorCodeToHttpStatusCode, | ||
} from './helper/http.helper.js' |
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
export type DCError<T> = { | ||
/** | ||
* The error message. | ||
*/ | ||
message: string | ||
|
||
/** | ||
* Additional details about the error. | ||
*/ | ||
details: string[] | ||
|
||
/** | ||
* The error code, typically uppercase and standardized. | ||
*/ | ||
code: number | string | ||
|
||
/** | ||
* Additional metadata about the error. | ||
*/ | ||
meta?: T | ||
} |
File renamed without changes.
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