@litert/core
Advanced tools
Comparing version 1.0.0 to 1.0.1
# Changes Logs | ||
## v1.0.1 | ||
- Allowed automatically generating code for defining new errors. | ||
- Now `IErrorHub.define` throws errors defined in the default error hub. | ||
## v1.0.0 | ||
@@ -4,0 +9,0 @@ |
@@ -125,6 +125,7 @@ /** | ||
* @param code The unique numeric-identity for the new error type. | ||
* The code is generated automatically if set to null. | ||
* @param name The unique string-identity for the new error type. | ||
* @param message The description for the new error type. | ||
*/ | ||
define<M2 extends M = M>(code: number, name: string, message: string): IErrorConstructor<M2>; | ||
define<M2 extends M = M>(code: number | null, name: string, message: string): IErrorConstructor<M2>; | ||
/** | ||
@@ -131,0 +132,0 @@ * Get the error constructor by its name . |
@@ -79,4 +79,6 @@ "use strict"; | ||
})(); | ||
let DEFAULT_HUB; | ||
class ErrorHub { | ||
constructor() { | ||
this._counter = 0; | ||
this._errors = {}; | ||
@@ -105,12 +107,30 @@ this._baseError = (Function("BaseError", `class __ extends BaseError { | ||
if (!/^[a-z]\w+$/i.test(name)) { | ||
throw new BaseError(1, "INVALID_ERROR_NAME", `Invalid name "${name}" for error definition.`); | ||
const TheError = DEFAULT_HUB.get("INVALID_ERROR_NAME"); | ||
throw new TheError({ | ||
message: `Invalid name ${JSON.stringify(name)} for error definition.` | ||
}); | ||
} | ||
if (!Number.isSafeInteger(code)) { | ||
throw new BaseError(2, "INVALID_ERROR_CODE", `Invalid code ${JSON.stringify(code)} for error definition.`); | ||
if (code === null) { | ||
code = this._counter++; | ||
} | ||
else if (!Number.isSafeInteger(code)) { | ||
const TheError = DEFAULT_HUB.get("INVALID_ERROR_CODE"); | ||
throw new TheError({ | ||
message: `Invalid code ${JSON.stringify(code)} for error definition.` | ||
}); | ||
} | ||
else { | ||
this._counter = code; | ||
} | ||
if (this._errors[name]) { | ||
throw new BaseError(3, "DUPLICATED_ERROR_NAME", `The name ${JSON.stringify(name)} of new error is duplicated.`); | ||
const TheError = DEFAULT_HUB.get("DUPLICATED_ERROR_NAME"); | ||
throw new TheError({ | ||
message: `The name ${JSON.stringify(name)} of new error already exists.` | ||
}); | ||
} | ||
if (this._errors[code]) { | ||
throw new BaseError(4, "DUPLICATED_ERROR_CODE", `The code ${JSON.stringify(code)} of new error is duplicated.`); | ||
const TheError = DEFAULT_HUB.get("DUPLICATED_ERROR_CODE"); | ||
throw new TheError({ | ||
message: `The code ${JSON.stringify(code)} of new error already exists.` | ||
}); | ||
} | ||
@@ -159,3 +179,7 @@ return this._errors[code] = this._errors[name] = (Function("BaseError", `class ${name} extends BaseError { | ||
exports.createErrorHub = createErrorHub; | ||
const DEFAULT_HUB = createErrorHub(); | ||
DEFAULT_HUB = createErrorHub(); | ||
DEFAULT_HUB.define(null, "INVALID_ERROR_NAME", `Invalid name for error definition.`); | ||
DEFAULT_HUB.define(null, "INVALID_ERROR_CODE", `Invalid code for error definition.`); | ||
DEFAULT_HUB.define(null, "DUPLICATED_ERROR_NAME", `The name of new error already exists.`); | ||
DEFAULT_HUB.define(null, "DUPLICATED_ERROR_CODE", `The code of new error already exists.`); | ||
/** | ||
@@ -162,0 +186,0 @@ * Get the default hub of errors. |
{ | ||
"name": "@litert/core", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "The core of LiteRT.", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -40,2 +40,8 @@ "use strict"; | ||
} | ||
try { | ||
myErrors.define(2, "MY_TEST_ERROR2", "Custom error 2"); | ||
} | ||
catch (e) { | ||
console.error(e.toString()); | ||
} | ||
//# sourceMappingURL=01-errors.js.map |
@@ -245,2 +245,3 @@ /** | ||
* @param code The unique numeric-identity for the new error type. | ||
* The code is generated automatically if set to null. | ||
* @param name The unique string-identity for the new error type. | ||
@@ -250,3 +251,3 @@ * @param message The description for the new error type. | ||
define<M2 extends M = M>( | ||
code: number, | ||
code: number | null, | ||
name: string, | ||
@@ -282,2 +283,4 @@ message: string | ||
let DEFAULT_HUB: IErrorHub<Record<string, any>>; | ||
class ErrorHub<M extends {}> | ||
@@ -290,4 +293,8 @@ implements IErrorHub<M> { | ||
private _counter: number; | ||
public constructor() { | ||
this._counter = 0; | ||
this._errors = {}; | ||
@@ -323,3 +330,3 @@ | ||
public define<M2 extends M>( | ||
code: number, | ||
code: number | null, | ||
name: string, | ||
@@ -331,25 +338,33 @@ message: string | ||
throw new BaseError( | ||
1, | ||
"INVALID_ERROR_NAME", | ||
`Invalid name "${name}" for error definition.` | ||
); | ||
const TheError = DEFAULT_HUB.get("INVALID_ERROR_NAME"); | ||
throw new TheError({ | ||
message: `Invalid name ${JSON.stringify(name)} for error definition.` | ||
}); | ||
} | ||
if (!Number.isSafeInteger(code)) { | ||
if (code === null) { | ||
throw new BaseError( | ||
2, | ||
"INVALID_ERROR_CODE", | ||
`Invalid code ${JSON.stringify(code)} for error definition.` | ||
); | ||
code = this._counter++; | ||
} | ||
else if (!Number.isSafeInteger(code)) { | ||
const TheError = DEFAULT_HUB.get("INVALID_ERROR_CODE"); | ||
throw new TheError({ | ||
message: `Invalid code ${JSON.stringify(code)} for error definition.` | ||
}); | ||
} | ||
else { | ||
this._counter = code; | ||
} | ||
if (this._errors[name]) { | ||
throw new BaseError( | ||
3, | ||
"DUPLICATED_ERROR_NAME", | ||
`The name ${JSON.stringify(name)} of new error is duplicated.` | ||
); | ||
const TheError = DEFAULT_HUB.get("DUPLICATED_ERROR_NAME"); | ||
throw new TheError({ | ||
message: `The name ${JSON.stringify(name)} of new error already exists.` | ||
}); | ||
} | ||
@@ -359,7 +374,7 @@ | ||
throw new BaseError( | ||
4, | ||
"DUPLICATED_ERROR_CODE", | ||
`The code ${JSON.stringify(code)} of new error is duplicated.` | ||
); | ||
const TheError = DEFAULT_HUB.get("DUPLICATED_ERROR_CODE"); | ||
throw new TheError({ | ||
message: `The code ${JSON.stringify(code)} of new error already exists.` | ||
}); | ||
} | ||
@@ -417,4 +432,28 @@ | ||
const DEFAULT_HUB = createErrorHub<DefaultMetadataType>(); | ||
DEFAULT_HUB = createErrorHub<DefaultMetadataType>(); | ||
DEFAULT_HUB.define( | ||
null, | ||
"INVALID_ERROR_NAME", | ||
`Invalid name for error definition.` | ||
); | ||
DEFAULT_HUB.define( | ||
null, | ||
"INVALID_ERROR_CODE", | ||
`Invalid code for error definition.` | ||
); | ||
DEFAULT_HUB.define( | ||
null, | ||
"DUPLICATED_ERROR_NAME", | ||
`The name of new error already exists.` | ||
); | ||
DEFAULT_HUB.define( | ||
null, | ||
"DUPLICATED_ERROR_CODE", | ||
`The code of new error already exists.` | ||
); | ||
/** | ||
@@ -421,0 +460,0 @@ * Get the default hub of errors. |
@@ -62,1 +62,14 @@ /** | ||
} | ||
try { | ||
myErrors.define<IMyErrorMetadata>( | ||
2, | ||
"MY_TEST_ERROR2", | ||
"Custom error 2" | ||
); | ||
} | ||
catch (e) { | ||
console.error(e.toString()); | ||
} |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
90926
1904
0