@poppinss/utils
Advanced tools
Comparing version 6.1.0-0 to 6.2.0-0
@@ -5,3 +5,3 @@ /// <reference types="node" resolution-mode="require"/> | ||
export { defineStaticProperty } from './src/define_static_property.js'; | ||
export { Exception } from './src/exception.js'; | ||
export { Exception, createError } from './src/exception.js'; | ||
export { flatten } from './src/flatten.js'; | ||
@@ -8,0 +8,0 @@ export { fsImportAll } from './src/fs_import_all.js'; |
@@ -6,3 +6,3 @@ import { fileURLToPath } from 'node:url'; | ||
export { defineStaticProperty } from './src/define_static_property.js'; | ||
export { Exception } from './src/exception.js'; | ||
export { Exception, createError } from './src/exception.js'; | ||
export { flatten } from './src/flatten.js'; | ||
@@ -9,0 +9,0 @@ export { fsImportAll } from './src/fs_import_all.js'; |
@@ -17,1 +17,6 @@ export declare class Exception extends Error { | ||
} | ||
export declare function createError<T extends any[] = never>(message: string, code: string, status?: number): typeof Exception & T extends never ? { | ||
new (args?: any, options?: ErrorOptions): Exception; | ||
} : { | ||
new (args: T, options?: ErrorOptions): Exception; | ||
}; |
@@ -0,1 +1,2 @@ | ||
import { format } from 'node:util'; | ||
export class Exception extends Error { | ||
@@ -30,1 +31,11 @@ name; | ||
} | ||
export function createError(message, code, status) { | ||
return class extends Exception { | ||
static message = message; | ||
static code = code; | ||
static status = status; | ||
constructor(args, options) { | ||
super(format(message, ...(args || [])), options); | ||
} | ||
}; | ||
} |
{ | ||
"name": "@poppinss/utils", | ||
"version": "6.1.0-0", | ||
"version": "6.2.0-0", | ||
"description": "Handy utilities for repetitive work", | ||
@@ -63,12 +63,12 @@ "main": "build/index.js", | ||
"@japa/spec-reporter": "^1.3.2", | ||
"@swc/core": "^1.3.20", | ||
"@swc/core": "^1.3.23", | ||
"@types/fs-extra": "^9.0.13", | ||
"@types/node": "^18.11.7", | ||
"@types/node": "^18.11.15", | ||
"c8": "^7.12.0", | ||
"del-cli": "^5.0.0", | ||
"eslint": "^8.28.0", | ||
"eslint": "^8.29.0", | ||
"eslint-config-prettier": "^8.5.0", | ||
"eslint-plugin-adonis": "^3.0.3", | ||
"eslint-plugin-prettier": "^4.2.1", | ||
"fs-extra": "^11.0.0", | ||
"fs-extra": "^11.1.0", | ||
"github-label-sync": "^2.2.0", | ||
@@ -80,5 +80,5 @@ "husky": "^8.0.1", | ||
"np": "^7.6.2", | ||
"prettier": "^2.8.0", | ||
"prettier": "^2.8.1", | ||
"ts-node": "^10.9.1", | ||
"typescript": "^4.9.3" | ||
"typescript": "^4.9.4" | ||
}, | ||
@@ -94,3 +94,3 @@ "dependencies": { | ||
"safe-stable-stringify": "^2.4.1", | ||
"secure-json-parse": "^2.5.0", | ||
"secure-json-parse": "^2.6.0", | ||
"slash": "^5.0.0", | ||
@@ -97,0 +97,0 @@ "slugify": "^1.6.5", |
@@ -774,15 +774,14 @@ # @poppinss/utils | ||
Following is an example of creating a dynamic message with interpolation. | ||
#### Anonymous error classes | ||
You can also create an anonymous exception class using the `createError` method. The return value is a class | ||
constructor that accepts an array of values to use for interpolation. | ||
The interpolation of error message is performed using the `util.format` message. | ||
```ts | ||
import { Exception } from '@poppinss/utils' | ||
import string from '@poppinss/utils/string' | ||
import { createError } from '@poppinss/utils' | ||
const E_RESOURCE_NOT_FOUND = createError('Unable to find resource with id %d', 'E_RESOURCE_NOT_FOUND') | ||
class ResourceNotFound extends Exception { | ||
static code = 'E_RESOURCE_NOT_FOUND' | ||
static status = 404 | ||
static message = 'Unable to find resource with id {{ id }}' | ||
} | ||
throw new ResourceNotFound(interpolate(ResourceNotFound.message, { id: 1 })) | ||
const id = 1 | ||
throw new E_RESOURCE_NOT_FOUND([id]) | ||
``` | ||
@@ -789,0 +788,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
94090
981
1086
Updatedsecure-json-parse@^2.6.0