@kamalyb/errors
Advanced tools
Comparing version 1.0.8 to 1.0.9
@@ -7,9 +7,10 @@ import { ValidationErrorItem } from 'joi'; | ||
path?: string; | ||
location?: Location; | ||
location?: ErrorLocation; | ||
} | ||
declare type Location = "body" | "params" | "query"; | ||
declare type CustomErrorParam = string | string[] | ErrorProps | ErrorProps[] | Array<string | ErrorProps>; | ||
type ErrorLocation = "body" | "params" | "query"; | ||
type CustomErrorParam = string | string[] | ErrorProps | ErrorProps[] | Array<string | ErrorProps>; | ||
type ErrorStatus = 400 | 401 | 403 | 404 | 409 | 422 | 429 | 500; | ||
declare const msg: (param: ValidationErrorItem[] | CustomErrorParam, concat?: boolean) => string; | ||
declare const msgs: { | ||
declare const messages: { | ||
403: string; | ||
@@ -21,11 +22,11 @@ 500: string; | ||
}; | ||
declare const setGlobalMessage: (t: Partial<typeof msgs>) => void; | ||
declare const ERRORS: readonly ["BadRequestError", "ForbiddenError", "InternalServerError", "JoiValidationError", "NotAuthorizedError", "NotFoundError", "RateLimitError", "UnprocessableEntityError", "CustomValidationError"]; | ||
declare type ICustomError = typeof ERRORS[number]; | ||
declare const setdefaultmessage: (t: Partial<typeof messages>) => void; | ||
declare const ERRORS: readonly ["BadRequestError", "ForbiddenError", "InternalServerError", "JoiValidationError", "NotAuthorizedError", "NotFoundError", "RateLimitError", "UnprocessableEntityError", "CustomValidationError", "ConflictError"]; | ||
type ICustomError = (typeof ERRORS)[number]; | ||
declare abstract class CustomError extends ExtendableError { | ||
abstract status: number; | ||
abstract status: ErrorStatus; | ||
abstract name: ICustomError; | ||
protected constructor(message: string); | ||
parse(param: CustomErrorParam, location?: Location): ErrorProps[]; | ||
parse(param: CustomErrorParam, location?: ErrorLocation): ErrorProps[]; | ||
abstract serialize(): ErrorProps[]; | ||
@@ -35,8 +36,7 @@ } | ||
declare class BadRequestError extends CustomError { | ||
status: number; | ||
readonly status = 400; | ||
readonly name = "BadRequestError"; | ||
path?: string; | ||
location?: Location; | ||
params: CustomErrorParam; | ||
constructor(message: string, location?: Location); | ||
private readonly location?; | ||
private readonly params; | ||
constructor(message: string, location?: ErrorLocation); | ||
constructor(messages: string[]); | ||
@@ -51,3 +51,3 @@ constructor(props: ErrorProps); | ||
message: string; | ||
status: number; | ||
readonly status = 403; | ||
readonly name = "ForbiddenError"; | ||
@@ -66,3 +66,3 @@ /** | ||
message: string; | ||
status: number; | ||
readonly status = 500; | ||
readonly name = "InternalServerError"; | ||
@@ -81,10 +81,10 @@ /** | ||
errors: ValidationErrorItem[]; | ||
location?: Location | undefined; | ||
status: number; | ||
location?: ErrorLocation | undefined; | ||
readonly status = 422; | ||
readonly name = "JoiValidationError"; | ||
constructor(errors: ValidationErrorItem[], location?: Location | undefined); | ||
constructor(errors: ValidationErrorItem[], location?: ErrorLocation | undefined); | ||
serialize(): { | ||
message: string; | ||
path: string; | ||
location: Location | undefined; | ||
location: ErrorLocation | undefined; | ||
}[]; | ||
@@ -94,5 +94,5 @@ } | ||
declare class NotAuthorizedError extends CustomError { | ||
status: number; | ||
readonly status = 401; | ||
readonly name = "NotAuthorizedError"; | ||
params: CustomErrorParam; | ||
private readonly params; | ||
/** | ||
@@ -108,3 +108,3 @@ * | ||
declare class NotFoundError extends CustomError { | ||
status: number; | ||
readonly status = 404; | ||
readonly name = "NotFoundError"; | ||
@@ -123,3 +123,3 @@ /** | ||
message: string; | ||
status: number; | ||
readonly status = 429; | ||
readonly name = "RateLimitError"; | ||
@@ -137,5 +137,5 @@ /** | ||
declare class ValidationError extends CustomError { | ||
status: number; | ||
readonly status = 422; | ||
readonly name = "CustomValidationError"; | ||
params: CustomErrorParam; | ||
private readonly params; | ||
constructor(params: ErrorProps | ErrorProps[]); | ||
@@ -146,5 +146,18 @@ serialize(): ErrorProps[]; | ||
declare class UnprocessableEntityError extends CustomError { | ||
status: number; | ||
readonly status = 422; | ||
readonly name = "UnprocessableEntityError"; | ||
params?: ErrorProps; | ||
private readonly params; | ||
private readonly location?; | ||
constructor(message: string, location?: ErrorLocation); | ||
constructor(messages: string[]); | ||
constructor(props: ErrorProps); | ||
constructor(props: ErrorProps[]); | ||
constructor(props: Array<string | ErrorProps>); | ||
serialize(): ErrorProps[]; | ||
} | ||
declare class ConflictError extends CustomError { | ||
readonly status = 409; | ||
readonly name = "ConflictError"; | ||
private readonly params; | ||
constructor(message: string); | ||
@@ -155,2 +168,2 @@ constructor(props: ErrorProps); | ||
export { BadRequestError, CustomError, CustomErrorParam, ERRORS, ErrorProps, ForbiddenError, ICustomError, InternalServerError, JoiValidationError, Location, NotAuthorizedError, NotFoundError, RateLimitError, UnprocessableEntityError, ValidationError, msg, msgs, setGlobalMessage }; | ||
export { BadRequestError, ConflictError, CustomError, CustomErrorParam, ERRORS, ErrorLocation, ErrorProps, ErrorStatus, ForbiddenError, ICustomError, InternalServerError, JoiValidationError, NotAuthorizedError, NotFoundError, RateLimitError, UnprocessableEntityError, ValidationError, messages, msg, setdefaultmessage }; |
@@ -0,1 +1,2 @@ | ||
"use strict"; | ||
var __defProp = Object.defineProperty; | ||
@@ -23,2 +24,3 @@ var __getOwnPropDesc = Object.getOwnPropertyDescriptor; | ||
BadRequestError: () => BadRequestError, | ||
ConflictError: () => ConflictError, | ||
CustomError: () => CustomError, | ||
@@ -34,5 +36,5 @@ ERRORS: () => ERRORS, | ||
ValidationError: () => ValidationError, | ||
messages: () => messages, | ||
msg: () => msg, | ||
msgs: () => msgs, | ||
setGlobalMessage: () => setGlobalMessage | ||
setdefaultmessage: () => setdefaultmessage | ||
}); | ||
@@ -52,3 +54,3 @@ module.exports = __toCommonJS(src_exports); | ||
}; | ||
var msgs = { | ||
var messages = { | ||
403: "thou shalt not", | ||
@@ -60,4 +62,4 @@ 500: "internal server error", | ||
}; | ||
var setGlobalMessage = (t) => { | ||
Object.assign(msgs, t); | ||
var setdefaultmessage = (t) => { | ||
Object.assign(messages, t); | ||
}; | ||
@@ -73,6 +75,7 @@ var ERRORS = [ | ||
"UnprocessableEntityError", | ||
"CustomValidationError" | ||
"CustomValidationError", | ||
"ConflictError" | ||
]; | ||
// src/errors/Custom.ts | ||
// src/errors/custom.ts | ||
var import_extendable_error = require("extendable-error"); | ||
@@ -84,6 +87,5 @@ var CustomError = class extends import_extendable_error.ExtendableError { | ||
parse(param, location) { | ||
if (typeof param === "string") { | ||
if (typeof param === "string") | ||
return [{ message: param, location }]; | ||
} | ||
if (Array.isArray(param)) { | ||
if (Array.isArray(param)) | ||
return param.map((p) => { | ||
@@ -101,3 +103,2 @@ if (typeof p === "string") { | ||
}); | ||
} | ||
return [param]; | ||
@@ -107,3 +108,3 @@ } | ||
// src/errors/BadRequest.ts | ||
// src/errors/bad-request.ts | ||
var BadRequestError = class extends CustomError { | ||
@@ -122,5 +123,9 @@ constructor(params, location) { | ||
// src/errors/Forbidden.ts | ||
// src/errors/forbidden.ts | ||
var ForbiddenError = class extends CustomError { | ||
constructor(message = msgs[403]) { | ||
/** | ||
* | ||
* @default thou shalt not | ||
*/ | ||
constructor(message = messages[403]) { | ||
super(message); | ||
@@ -136,5 +141,9 @@ this.message = message; | ||
// src/errors/InternalServer.ts | ||
// src/errors/internal-server.ts | ||
var InternalServerError = class extends CustomError { | ||
constructor(message = msgs[500]) { | ||
/** | ||
* | ||
* @default internal server error | ||
*/ | ||
constructor(message = messages[500]) { | ||
super(message); | ||
@@ -150,3 +159,3 @@ this.message = message; | ||
// src/errors/JoiValidation.ts | ||
// src/errors/joi-validation.ts | ||
var JoiValidationError = class extends CustomError { | ||
@@ -161,16 +170,13 @@ constructor(errors, location) { | ||
serialize() { | ||
return this.errors.map((error) => { | ||
var _a; | ||
return { | ||
message: error.message, | ||
path: (_a = error.path[0]) == null ? void 0 : _a.toString(), | ||
location: this.location | ||
}; | ||
}); | ||
return this.errors.map((error) => ({ | ||
message: error.message, | ||
path: error.path[0]?.toString(), | ||
location: this.location | ||
})); | ||
} | ||
}; | ||
// src/errors/NotAuthorized.ts | ||
// src/errors/not-authorized.ts | ||
var NotAuthorizedError = class extends CustomError { | ||
constructor(t = msgs[401]) { | ||
constructor(t = messages[401]) { | ||
super(typeof t === "string" ? t : t.message); | ||
@@ -186,5 +192,9 @@ this.status = 401; | ||
// src/errors/NotFound.ts | ||
// src/errors/not-found.ts | ||
var NotFoundError = class extends CustomError { | ||
constructor(message = msgs[404]) { | ||
/** | ||
* | ||
* @default not found | ||
*/ | ||
constructor(message = messages[404]) { | ||
super(message); | ||
@@ -199,5 +209,9 @@ this.status = 404; | ||
// src/errors/RateLimit.ts | ||
// src/errors/rate-limit.ts | ||
var RateLimitError = class extends CustomError { | ||
constructor(message = msgs[429]) { | ||
/** | ||
* | ||
* @default too many requests. please try again later | ||
*/ | ||
constructor(message = messages[429]) { | ||
super(message); | ||
@@ -213,3 +227,3 @@ this.message = message; | ||
// src/errors/Validation.ts | ||
// src/errors/validation.ts | ||
var ValidationError = class extends CustomError { | ||
@@ -227,18 +241,32 @@ constructor(params) { | ||
// src/errors/UnprocessableEntityError.ts | ||
// src/errors/unprocessable-entity.ts | ||
var UnprocessableEntityError = class extends CustomError { | ||
constructor(props) { | ||
super(typeof props === "string" ? props : props.message); | ||
constructor(params, location) { | ||
super(msg(params)); | ||
this.status = 422; | ||
this.name = "UnprocessableEntityError"; | ||
if (typeof props !== "string") | ||
this.params = props; | ||
this.params = params; | ||
this.location = location; | ||
} | ||
serialize() { | ||
return this.params ? [this.params] : [{ message: this.message }]; | ||
return this.parse(this.params, this.location); | ||
} | ||
}; | ||
// src/errors/conflict.ts | ||
var ConflictError = class extends CustomError { | ||
constructor(params) { | ||
super(msg(params)); | ||
this.status = 409; | ||
this.name = "ConflictError"; | ||
this.params = params; | ||
} | ||
serialize() { | ||
return this.parse(this.params); | ||
} | ||
}; | ||
// Annotate the CommonJS export names for ESM import in node: | ||
0 && (module.exports = { | ||
BadRequestError, | ||
ConflictError, | ||
CustomError, | ||
@@ -254,5 +282,5 @@ ERRORS, | ||
ValidationError, | ||
messages, | ||
msg, | ||
msgs, | ||
setGlobalMessage | ||
setdefaultmessage | ||
}); |
{ | ||
"name": "@kamalyb/errors", | ||
"version": "1.0.8", | ||
"version": "1.0.9", | ||
"description": "errors", | ||
@@ -34,19 +34,19 @@ "publishConfig": { | ||
"devDependencies": { | ||
"@typescript-eslint/eslint-plugin": "^5.30.6", | ||
"@typescript-eslint/parser": "^5.30.6", | ||
"eslint": "^8.19.0", | ||
"@typescript-eslint/eslint-plugin": "^5.54.1", | ||
"@typescript-eslint/parser": "^5.54.1", | ||
"eslint": "^8.36.0", | ||
"eslint-config-airbnb-base": "^15.0.0", | ||
"eslint-config-airbnb-typescript": "^17.0.0", | ||
"eslint-config-prettier": "^8.5.0", | ||
"eslint-plugin-import": "^2.25.4", | ||
"eslint-config-prettier": "^8.7.0", | ||
"eslint-plugin-import": "^2.27.5", | ||
"eslint-plugin-prettier": "^4.2.1", | ||
"prettier": "2.7.1", | ||
"tsup": "6.1.3", | ||
"typescript": "4.7.4", | ||
"vitest": "0.18.0" | ||
"prettier": "2.8.4", | ||
"tsup": "6.6.3", | ||
"typescript": "4.9.5", | ||
"vitest": "0.29.2" | ||
}, | ||
"dependencies": { | ||
"extendable-error": "^0.1.7", | ||
"joi": "^17.6.0" | ||
"joi": "^17.8.3" | ||
} | ||
} |
@@ -1,10 +0,11 @@ | ||
export * from "./BadRequest"; | ||
export * from "./Custom"; | ||
export * from "./Forbidden"; | ||
export * from "./InternalServer"; | ||
export * from "./JoiValidation"; | ||
export * from "./NotAuthorized"; | ||
export * from "./NotFound"; | ||
export * from "./RateLimit"; | ||
export * from "./Validation"; | ||
export * from "./UnprocessableEntityError"; | ||
export * from "./bad-request"; | ||
export * from "./custom"; | ||
export * from "./forbidden"; | ||
export * from "./internal-server"; | ||
export * from "./joi-validation"; | ||
export * from "./not-authorized"; | ||
export * from "./not-found"; | ||
export * from "./rate-limit"; | ||
export * from "./validation"; | ||
export * from "./unprocessable-entity"; | ||
export * from "./conflict"; |
export interface ErrorProps { | ||
message: string; | ||
path?: string; | ||
location?: Location; | ||
location?: ErrorLocation; | ||
} | ||
export type Location = "body" | "params" | "query"; | ||
export type ErrorLocation = "body" | "params" | "query"; | ||
@@ -15,1 +15,3 @@ export type CustomErrorParam = | ||
| Array<string | ErrorProps>; | ||
export type ErrorStatus = 400 | 401 | 403 | 404 | 409 | 422 | 429 | 500; |
@@ -1,3 +0,3 @@ | ||
import { ValidationErrorItem } from "joi"; | ||
import { CustomErrorParam } from "../types"; | ||
import type { ValidationErrorItem } from "joi"; | ||
import type { CustomErrorParam } from "../types"; | ||
@@ -7,3 +7,3 @@ export const msg = ( | ||
concat = true | ||
): string => { | ||
) => { | ||
if (typeof param === "string") return param; | ||
@@ -21,3 +21,3 @@ | ||
export const msgs = { | ||
export const messages = { | ||
403: "thou shalt not", | ||
@@ -30,4 +30,4 @@ 500: "internal server error", | ||
export const setGlobalMessage = (t: Partial<typeof msgs>) => { | ||
Object.assign(msgs, t); | ||
export const setdefaultmessage = (t: Partial<typeof messages>) => { | ||
Object.assign(messages, t); | ||
}; | ||
@@ -44,5 +44,6 @@ | ||
"UnprocessableEntityError", | ||
"CustomValidationError" | ||
"CustomValidationError", | ||
"ConflictError" | ||
] as const; | ||
export type ICustomError = typeof ERRORS[number]; | ||
export type ICustomError = (typeof ERRORS)[number]; |
Sorry, the diff of this file is not supported yet
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
26491
19
895
Updatedjoi@^17.8.3