@litert/core
Advanced tools
Comparing version 1.0.5 to 1.0.6
# Changes Logs | ||
## v1.0.6 | ||
- Added alias supports for error codes and names. | ||
## v1.0.5 | ||
@@ -4,0 +8,0 @@ |
@@ -27,2 +27,6 @@ /** | ||
/** | ||
* The other code of this error. | ||
*/ | ||
aliasCodes: number[]; | ||
/** | ||
* The string-code of an error, as the name, used to identify the type or | ||
@@ -33,2 +37,6 @@ * the reason of the error. | ||
/** | ||
* The other names of this error. | ||
*/ | ||
aliases: string[]; | ||
/** | ||
* The detail description of an error, used to describe the details of the | ||
@@ -66,2 +74,6 @@ * error. | ||
/** | ||
* The other code of this error. | ||
*/ | ||
readonly aliasCodes: number[]; | ||
/** | ||
* The string-code of an error, as the name, used to identify the type or | ||
@@ -72,2 +84,6 @@ * the reason of the error. | ||
/** | ||
* The other names of this error. | ||
*/ | ||
readonly aliases: string[]; | ||
/** | ||
* The detail description of an error, used to describe the details of the | ||
@@ -119,2 +135,6 @@ * error. | ||
/** | ||
* The other names of this error. | ||
*/ | ||
readonly aliases: string[]; | ||
/** | ||
* The default description of the error. | ||
@@ -128,2 +148,6 @@ */ | ||
/** | ||
* The other code of this error. | ||
*/ | ||
readonly aliasCodes: number[]; | ||
/** | ||
* The name of module thats emit this error. | ||
@@ -154,10 +178,13 @@ */ | ||
* @param message The description for the new error type. | ||
* @param metadata The metadata of this new error. | ||
* @param aliasCodes The alias codes of this new error. | ||
* @param aliases The alias names of this new error. | ||
*/ | ||
define<M2 extends M = M>(code: number | null, name: string, message: string, metadata: M2): IErrorConstructor<M2>; | ||
define<M2 extends M = M>(code: number | null, name: string, message: string, metadata: M2, aliasCodes?: number[], aliases?: string[]): IErrorConstructor<M2>; | ||
/** | ||
* Get the error constructor by its name . | ||
* Get the error constructor by its name or code. | ||
* | ||
* @param name The string-identity for the error type. | ||
* @param identity The string-identity or code-identity for the error type. | ||
*/ | ||
get<M2 extends M = M>(name: string): IErrorConstructor<M2>; | ||
get<M2 extends M = M>(identity: string | number): IErrorConstructor<M2>; | ||
/** | ||
@@ -164,0 +191,0 @@ * Check if an error belongs to an error type defined in this hub. |
@@ -51,2 +51,12 @@ "use strict"; | ||
"value": {} | ||
}, | ||
"aliasCodes": { | ||
"writable": false, | ||
"configurable": false, | ||
"value": aliasCodes | ||
}, | ||
"aliases": { | ||
"writable": false, | ||
"configurable": false, | ||
"value": aliases | ||
} | ||
@@ -65,3 +75,3 @@ }); | ||
} | ||
let ret = Function("code", "name", "message", "metadata", "moduleName", THE_CONSTRUCTOR); | ||
let ret = Function("code", "name", "message", "metadata", "moduleName", "aliasCodes", "aliases", THE_CONSTRUCTOR); | ||
ret.prototype.getStackAsArray = function () { | ||
@@ -73,2 +83,3 @@ return this.stack.split(/\n\s+at /).slice(1); | ||
"code": this.code, | ||
"aliasCodes": this.aliasCodes, | ||
"message": this.message, | ||
@@ -78,8 +89,11 @@ "metadata": this.metadata, | ||
"name": this.name, | ||
"aliases": this.aliases, | ||
"stack": this.getStackAsArray() | ||
} : { | ||
"code": this.code, | ||
"aliasCodes": this.aliasCodes, | ||
"message": this.message, | ||
"metadata": this.metadata, | ||
"module": this.module, | ||
"aliases": this.aliases, | ||
"name": this.name | ||
@@ -102,3 +116,3 @@ }; | ||
this._baseError = (Function("BaseError", `class __ extends BaseError { | ||
constructor(code, name, message, metadata, moduleName) { | ||
constructor(code, name, message, metadata, moduleName, aliasCodes, aliases) { | ||
super( | ||
@@ -109,3 +123,5 @@ code, | ||
metadata, | ||
moduleName | ||
moduleName, | ||
aliasCodes, | ||
aliases | ||
); | ||
@@ -127,3 +143,3 @@ }; | ||
} | ||
define(code, name, message, metadata) { | ||
define(code, name, message, metadata, aliasCodes = [], aliases = []) { | ||
if (!/^[a-z]\w+$/i.test(name)) { | ||
@@ -153,2 +169,12 @@ const TheError = DEFAULT_HUB.get("INVALID_ERROR_NAME"); | ||
} | ||
if (aliases.length) { | ||
for (const alias of aliases) { | ||
if (this._errors[alias]) { | ||
const TheError = DEFAULT_HUB.get("DUPLICATED_ERROR_NAME"); | ||
throw new TheError({ | ||
message: `The name ${JSON.stringify(alias)} of new error already exists.` | ||
}); | ||
} | ||
} | ||
} | ||
if (this._errors[code]) { | ||
@@ -160,3 +186,13 @@ const TheError = DEFAULT_HUB.get("DUPLICATED_ERROR_CODE"); | ||
} | ||
return this._errors[code] = this._errors[name] = (Function("BaseError", "metadata", `class ${name} extends BaseError { | ||
if (aliasCodes.length) { | ||
for (const alias of aliasCodes) { | ||
if (this._errors[alias]) { | ||
const TheError = DEFAULT_HUB.get("DUPLICATED_ERROR_CODE"); | ||
throw new TheError({ | ||
message: `The code ${JSON.stringify(alias)} of new error already exists.` | ||
}); | ||
} | ||
} | ||
} | ||
this._errors[code] = this._errors[name] = (Function("BaseError", "metadata", `class ${name} extends BaseError { | ||
constructor(opts = {}) { | ||
@@ -168,3 +204,5 @@ super( | ||
{ ...metadata, ...opts.metadata }, | ||
${JSON.stringify(this._module)} | ||
${JSON.stringify(this._module)}, | ||
${JSON.stringify(aliasCodes)}, | ||
${JSON.stringify(aliases)} | ||
); | ||
@@ -195,2 +233,12 @@ }; | ||
}, | ||
"aliases": { | ||
"writable": false, | ||
"configurable": false, | ||
"value": ${JSON.stringify(aliases)} | ||
}, | ||
"aliasCodes": { | ||
"writable": false, | ||
"configurable": false, | ||
"value": ${JSON.stringify(aliasCodes)} | ||
}, | ||
"defaultMetadata": { | ||
@@ -204,2 +252,13 @@ "writable": false, | ||
return ${name};`))(this._baseError, metadata); | ||
if (aliasCodes) { | ||
for (const alias of aliasCodes) { | ||
this._errors[alias] = this._errors[code]; | ||
} | ||
} | ||
if (aliases) { | ||
for (const alias of aliases) { | ||
this._errors[alias] = this._errors[code]; | ||
} | ||
} | ||
return this._errors[code]; | ||
} | ||
@@ -206,0 +265,0 @@ get(name) { |
{ | ||
"name": "@litert/core", | ||
"version": "1.0.5", | ||
"version": "1.0.6", | ||
"description": "The core of LiteRT.", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -35,3 +35,3 @@ "use strict"; | ||
"source": "China" | ||
}); | ||
}, [1234], ["HEIHEIHEI"]); | ||
console.error(JSON.stringify(new MY_TEST_ERROR1({ | ||
@@ -44,4 +44,7 @@ "metadata": { | ||
console.log(MY_TEST_ERROR2.message); | ||
console.log(MY_TEST_ERROR2.aliasCodes); | ||
console.log(MY_TEST_ERROR2.aliases); | ||
console.log(new MY_TEST_ERROR2().metadata.source); | ||
console.log(new MY_TEST_ERROR2({ "metadata": { "source": "Japan" } }).metadata.source); | ||
console.log(new MY_TEST_ERROR2({ "metadata": { "source": "Japan" } }).aliasCodes); | ||
} | ||
@@ -61,2 +64,4 @@ try { | ||
console.log(`Error[MY_TEST_ERROR2] Source: ${myErrors.get("MY_TEST_ERROR2").defaultMetadata.source}`); | ||
console.log(`Error[1234] Name: ${myErrors.get(1234).name}`); | ||
console.log(`Error[HEIHEIHEI] Name: ${myErrors.get("HEIHEIHEI").name}`); | ||
//# sourceMappingURL=01-errors.js.map |
@@ -31,2 +31,7 @@ /** | ||
/** | ||
* The other code of this error. | ||
*/ | ||
aliasCodes: number[]; | ||
/** | ||
* The string-code of an error, as the name, used to identify the type or | ||
@@ -38,2 +43,7 @@ * the reason of the error. | ||
/** | ||
* The other names of this error. | ||
*/ | ||
aliases: string[]; | ||
/** | ||
* The detail description of an error, used to describe the details of the | ||
@@ -78,2 +88,7 @@ * error. | ||
/** | ||
* The other code of this error. | ||
*/ | ||
readonly aliasCodes: number[]; | ||
/** | ||
* The string-code of an error, as the name, used to identify the type or | ||
@@ -85,2 +100,7 @@ * the reason of the error. | ||
/** | ||
* The other names of this error. | ||
*/ | ||
readonly aliases: string[]; | ||
/** | ||
* The detail description of an error, used to describe the details of the | ||
@@ -160,2 +180,7 @@ * error. | ||
/** | ||
* The other names of this error. | ||
*/ | ||
readonly aliases: string[]; | ||
/** | ||
* The default description of the error. | ||
@@ -171,2 +196,7 @@ */ | ||
/** | ||
* The other code of this error. | ||
*/ | ||
readonly aliasCodes: number[]; | ||
/** | ||
* The name of module thats emit this error. | ||
@@ -216,2 +246,12 @@ */ | ||
"value": {} | ||
}, | ||
"aliasCodes": { | ||
"writable": false, | ||
"configurable": false, | ||
"value": aliasCodes | ||
}, | ||
"aliases": { | ||
"writable": false, | ||
"configurable": false, | ||
"value": aliases | ||
} | ||
@@ -235,3 +275,10 @@ }); | ||
let ret = Function( | ||
"code", "name", "message", "metadata", "moduleName", THE_CONSTRUCTOR | ||
"code", | ||
"name", | ||
"message", | ||
"metadata", | ||
"moduleName", | ||
"aliasCodes", | ||
"aliases", | ||
THE_CONSTRUCTOR | ||
); | ||
@@ -251,2 +298,3 @@ | ||
"code": this.code, | ||
"aliasCodes": this.aliasCodes, | ||
"message": this.message, | ||
@@ -256,8 +304,11 @@ "metadata": this.metadata, | ||
"name": this.name, | ||
"aliases": this.aliases, | ||
"stack": this.getStackAsArray() | ||
} : { | ||
"code": this.code, | ||
"aliasCodes": this.aliasCodes, | ||
"message": this.message, | ||
"metadata": this.metadata, | ||
"module": this.module, | ||
"aliases": this.aliases, | ||
"name": this.name | ||
@@ -296,2 +347,5 @@ }; | ||
* @param message The description for the new error type. | ||
* @param metadata The metadata of this new error. | ||
* @param aliasCodes The alias codes of this new error. | ||
* @param aliases The alias names of this new error. | ||
*/ | ||
@@ -302,12 +356,14 @@ define<M2 extends M = M>( | ||
message: string, | ||
metadata: M2 | ||
metadata: M2, | ||
aliasCodes?: number[], | ||
aliases?: string[] | ||
): IErrorConstructor<M2>; | ||
/** | ||
* Get the error constructor by its name . | ||
* Get the error constructor by its name or code. | ||
* | ||
* @param name The string-identity for the error type. | ||
* @param identity The string-identity or code-identity for the error type. | ||
*/ | ||
get<M2 extends M = M>( | ||
name: string | ||
identity: string | number | ||
): IErrorConstructor<M2>; | ||
@@ -355,3 +411,3 @@ | ||
"BaseError", `class __ extends BaseError { | ||
constructor(code, name, message, metadata, moduleName) { | ||
constructor(code, name, message, metadata, moduleName, aliasCodes, aliases) { | ||
super( | ||
@@ -362,3 +418,5 @@ code, | ||
metadata, | ||
moduleName | ||
moduleName, | ||
aliasCodes, | ||
aliases | ||
); | ||
@@ -392,3 +450,5 @@ }; | ||
message: string, | ||
metadata?: M2 | ||
metadata?: M2, | ||
aliasCodes: number[] = [], | ||
aliases: string[] = [] | ||
): IErrorConstructor<M2> { | ||
@@ -431,2 +491,17 @@ | ||
if (aliases.length) { | ||
for (const alias of aliases) { | ||
if (this._errors[alias]) { | ||
const TheError = DEFAULT_HUB.get("DUPLICATED_ERROR_NAME"); | ||
throw new TheError({ | ||
message: `The name ${JSON.stringify(alias)} of new error already exists.` | ||
}); | ||
} | ||
} | ||
} | ||
if (this._errors[code]) { | ||
@@ -441,3 +516,18 @@ | ||
return this._errors[code] = this._errors[name] = (Function( | ||
if (aliasCodes.length) { | ||
for (const alias of aliasCodes) { | ||
if (this._errors[alias]) { | ||
const TheError = DEFAULT_HUB.get("DUPLICATED_ERROR_CODE"); | ||
throw new TheError({ | ||
message: `The code ${JSON.stringify(alias)} of new error already exists.` | ||
}); | ||
} | ||
} | ||
} | ||
this._errors[code] = this._errors[name] = (Function( | ||
"BaseError", "metadata", `class ${name} extends BaseError { | ||
@@ -450,3 +540,5 @@ constructor(opts = {}) { | ||
{ ...metadata, ...opts.metadata }, | ||
${JSON.stringify(this._module)} | ||
${JSON.stringify(this._module)}, | ||
${JSON.stringify(aliasCodes)}, | ||
${JSON.stringify(aliases)} | ||
); | ||
@@ -477,2 +569,12 @@ }; | ||
}, | ||
"aliases": { | ||
"writable": false, | ||
"configurable": false, | ||
"value": ${JSON.stringify(aliases)} | ||
}, | ||
"aliasCodes": { | ||
"writable": false, | ||
"configurable": false, | ||
"value": ${JSON.stringify(aliasCodes)} | ||
}, | ||
"defaultMetadata": { | ||
@@ -487,2 +589,20 @@ "writable": false, | ||
))(this._baseError, metadata) as any; | ||
if (aliasCodes) { | ||
for (const alias of aliasCodes) { | ||
this._errors[alias] = this._errors[code]; | ||
} | ||
} | ||
if (aliases) { | ||
for (const alias of aliases) { | ||
this._errors[alias] = this._errors[code]; | ||
} | ||
} | ||
return this._errors[code]; | ||
} | ||
@@ -489,0 +609,0 @@ |
@@ -55,3 +55,5 @@ /** | ||
"source": "China" | ||
} | ||
}, | ||
[1234], | ||
["HEIHEIHEI"] | ||
); | ||
@@ -68,4 +70,7 @@ | ||
console.log(MY_TEST_ERROR2.message); | ||
console.log(MY_TEST_ERROR2.aliasCodes); | ||
console.log(MY_TEST_ERROR2.aliases); | ||
console.log(new MY_TEST_ERROR2().metadata.source); | ||
console.log(new MY_TEST_ERROR2({ "metadata": { "source": "Japan" } }).metadata.source); | ||
console.log(new MY_TEST_ERROR2({ "metadata": { "source": "Japan" } }).aliasCodes); | ||
} | ||
@@ -98,1 +103,5 @@ | ||
console.log(`Error[MY_TEST_ERROR2] Source: ${myErrors.get("MY_TEST_ERROR2").defaultMetadata.source}`); | ||
console.log(`Error[1234] Name: ${myErrors.get(1234).name}`); | ||
console.log(`Error[HEIHEIHEI] Name: ${myErrors.get("HEIHEIHEI").name}`); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
120463
2547