code-added-error
Advanced tools
Comparing version 0.1.2 to 0.1.3
@@ -1,2 +0,2 @@ | ||
"use strict";module.exports=class CodeAddedError extends Error{constructor(message="",code="",fileName="",lineNumber=0){super(message);this.name="CodeAddedError";this.code=code.toUpperCase();this.fileName=fileName;this.lineNumber=lineNumber;if("string"===typeof message){this.message=message}else if(message instanceof Error){this.message=message.message}else if(message&&message.message){this.message=message.message}else{this.message=JSON.stringify(message)}}toJSON(codeAndMessageOnly=false){return codeAndMessageOnly?{code:this.code,message:this.message}:{code:this.code,message:this.message,fileName:this.fileName,lineNumber:this.lineNumber}}toString(codeAndMessageOnly=false){let fileData="";fileData+=this.code?"["+this.code+"]":"";fileData+=this.code&&this.message?" - ":"";fileData+=this.message;if(!codeAndMessageOnly){if(this.fileName||this.lineNumber){fileData+=" (";if(this.fileName){fileData+="file: "+this.fileName}if(this.lineNumber){fileData+=(this.fileName?", ":"")+"line: "+this.lineNumber}fileData+=")"}}return fileData}static file(){let file="";let stack=(new Error).stack.toString();if(stack){stack=stack.split(/\r\n|\n/);if(2<stack.length){stack=stack[2].trim();const startAt=stack.indexOf("(");if(-1<startAt){stack=stack.slice(startAt+1,stack.length-1);stack=stack.split(":");if(3<=stack.length){file=stack.slice(0,stack.length-2).join(":")}}}}return file}static line(){let line=0;let stack=(new Error).stack.toString();if(stack){stack=stack.split(/\r\n|\n/);if(2<stack.length){stack=stack[2].trim().split(":");if(2<=stack.length){line=stack[stack.length-2]}}}return 0<line?parseInt(line,10):0}}; | ||
"use strict";module.exports=class CodeAddedError extends Error{constructor(error="",code="",fileName="",lineNumber=0){super(error&&error.message?error.message:error);this.name="CodeAddedError";this.fileName=fileName;this.lineNumber=lineNumber;if("string"===typeof error){this.message=error}else if(error instanceof Error){this.message=error.message;if(!code){if(error instanceof ReferenceError){this.code="MISSING"}else if(error instanceof TypeError){this.code="WRONG_TYPE"}else if(error instanceof RangeError){this.code="RANGE"}else{this.code="UNKNOWN"}}}else if(error&&error.message){this.message=error.message;if(error.code){this.code=String(error.code).toUpperCase()}}else{this.message=JSON.stringify(error)}if(!this.code){this.code=String(code).toUpperCase()}}toJSON(codeAndMessageOnly=false){return codeAndMessageOnly?{code:this.code,message:this.message}:{code:this.code,message:this.message,fileName:this.fileName,lineNumber:this.lineNumber}}toString(codeAndMessageOnly=false){let fileData="";fileData+=this.code?"["+this.code+"]":"";fileData+=this.code&&this.message?" - ":"";fileData+=this.message;if(!codeAndMessageOnly){if(this.fileName||this.lineNumber){fileData+=" (";if(this.fileName){fileData+="file: "+this.fileName}if(this.lineNumber){fileData+=(this.fileName?", ":"")+"line: "+this.lineNumber}fileData+=")"}}return fileData}static file(){let file="";let stack=(new Error).stack.toString();if(stack){stack=stack.split(/\r\n|\n/);if(2<stack.length){stack=stack[2].trim();const startAt=stack.indexOf("(");if(-1<startAt){stack=stack.slice(startAt+1,stack.length-1);stack=stack.split(":");if(3<=stack.length){file=stack.slice(0,stack.length-2).join(":")}}}}return file}static line(){let line=0;let stack=(new Error).stack.toString();if(stack){stack=stack.split(/\r\n|\n/);if(2<stack.length){stack=stack[2].trim().split(":");if(2<=stack.length){line=stack[stack.length-2]}}}return 0<line?parseInt(line,10):0}}; | ||
//# sourceMappingURL=code-added-error.min.js.map |
@@ -7,24 +7,52 @@ "use strict"; | ||
constructor (message = "", code = "", fileName = "", lineNumber = 0) { | ||
constructor (error = "", code = "", fileName = "", lineNumber = 0) { | ||
super(message); | ||
super(error && error.message ? error.message : error); | ||
this.name = "CodeAddedError"; | ||
this.code = code.toUpperCase(); | ||
this.fileName = fileName; | ||
this.lineNumber = lineNumber; | ||
if ("string" === typeof message) { | ||
this.message = message; | ||
if ("string" === typeof error) { | ||
this.message = error; | ||
} | ||
else if (message instanceof Error) { | ||
this.message = message.message; | ||
else if (error instanceof Error) { | ||
this.message = error.message; | ||
if (!code) { | ||
if (error instanceof ReferenceError) { | ||
this.code = "MISSING"; | ||
} | ||
else if (error instanceof TypeError) { | ||
this.code = "WRONG_TYPE"; | ||
} | ||
else if (error instanceof RangeError) { | ||
this.code = "RANGE"; | ||
} | ||
else { | ||
this.code = "UNKNOWN"; | ||
} | ||
} | ||
} | ||
else if (message && message.message) { | ||
this.message = message.message; | ||
else if (error && error.message) { | ||
this.message = error.message; | ||
if (error.code) { | ||
this.code = String(error.code).toUpperCase(); | ||
} | ||
} | ||
else { | ||
this.message = JSON.stringify(message); | ||
this.message = JSON.stringify(error); | ||
} | ||
if (!this.code) { | ||
this.code = String(code).toUpperCase(); | ||
} | ||
} | ||
@@ -31,0 +59,0 @@ |
{ | ||
"name": "code-added-error", | ||
"version": "0.1.2", | ||
"version": "0.1.3", | ||
"description": "Emulation of basic Error class, with new \"code\" argument and \"file\" & \"line\" static methods", | ||
@@ -37,12 +37,12 @@ "main": "lib/code-added-error.js", | ||
"devDependencies": { | ||
"@types/node": "12.0.2", | ||
"@types/node": "12.0.10", | ||
"check-version-modules": "1.1.1", | ||
"coveralls": "3.0.3", | ||
"eslint": "5.16.0", | ||
"husky": "2.3.0", | ||
"coveralls": "3.0.4", | ||
"eslint": "6.0.1", | ||
"husky": "3.0.0", | ||
"mocha": "6.1.4", | ||
"nyc": "14.1.1", | ||
"terser": "4.0.0" | ||
"terser": "4.0.2" | ||
}, | ||
"homepage": "https://github.com/Psychopoulet/code-added-error#readme" | ||
} |
@@ -29,3 +29,3 @@ | ||
exec("tsc " + join(__dirname, "typescript", "compilation.ts"), { | ||
exec("tsc " + join(__dirname, "typescript", "compilation.ts") + " --target ES2015", { | ||
"cwd": join(__dirname, ".."), | ||
@@ -32,0 +32,0 @@ "windowsHide": true |
@@ -72,2 +72,65 @@ "use strict"; | ||
it("should test constructor with object message", () => { | ||
const err = new CodeAddedError({ | ||
"message": "This is a test", | ||
"code": "TEST" | ||
}); | ||
assert.strictEqual(typeof err.message, "string", "It does not generate the wanted Error"); | ||
assert.strictEqual(err.message, "This is a test", "It does not generate the wanted Error"); | ||
assert.strictEqual(typeof err.code, "string", "It does not generate the wanted Error"); | ||
assert.strictEqual(err.code, "TEST", "It does not generate the wanted Error"); | ||
}); | ||
it("should test constructor with Error message", () => { | ||
const err = new CodeAddedError(new Error("This is a test")); | ||
assert.strictEqual(typeof err.message, "string", "It does not generate the wanted Error"); | ||
assert.strictEqual(err.message, "This is a test", "It does not generate the wanted Error"); | ||
assert.strictEqual(typeof err.code, "string", "It does not generate the wanted Error"); | ||
assert.strictEqual(err.code, "UNKNOWN", "It does not generate the wanted Error"); | ||
}); | ||
it("should test constructor with ReferenceError message", () => { | ||
const err = new CodeAddedError(new ReferenceError("This is a test")); | ||
assert.strictEqual(typeof err.message, "string", "It does not generate the wanted Error"); | ||
assert.strictEqual(err.message, "This is a test", "It does not generate the wanted Error"); | ||
assert.strictEqual(typeof err.code, "string", "It does not generate the wanted Error"); | ||
assert.strictEqual(err.code, "MISSING", "It does not generate the wanted Error"); | ||
}); | ||
it("should test constructor with TypeError message", () => { | ||
const err = new CodeAddedError(new TypeError("This is a test")); | ||
assert.strictEqual(typeof err.message, "string", "It does not generate the wanted Error"); | ||
assert.strictEqual(err.message, "This is a test", "It does not generate the wanted Error"); | ||
assert.strictEqual(typeof err.code, "string", "It does not generate the wanted Error"); | ||
assert.strictEqual(err.code, "WRONG_TYPE", "It does not generate the wanted Error"); | ||
}); | ||
it("should test constructor with RangeError message", () => { | ||
const err = new CodeAddedError(new RangeError("This is a test")); | ||
assert.strictEqual(typeof err.message, "string", "It does not generate the wanted Error"); | ||
assert.strictEqual(err.message, "This is a test", "It does not generate the wanted Error"); | ||
assert.strictEqual(typeof err.code, "string", "It does not generate the wanted Error"); | ||
assert.strictEqual(err.code, "RANGE", "It does not generate the wanted Error"); | ||
}); | ||
it("should test constructor with Error message & code", () => { | ||
@@ -80,2 +143,5 @@ | ||
assert.strictEqual(typeof err.code, "string", "It does not generate the wanted Error"); | ||
assert.strictEqual(err.code, "TEST", "It does not generate the wanted Error"); | ||
}); | ||
@@ -82,0 +148,0 @@ |
/// <reference path="../../lib/index.d.ts" /> | ||
import CodeAddedError = require('../../lib/code-added-error.js'); | ||
import * as CodeAddedError from '../../lib/code-added-error.js'; | ||
@@ -5,0 +5,0 @@ console.log(CodeAddedError); |
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
40273
737