-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
unknown
committed
Dec 18, 2018
0 parents
commit a5c251a
Showing
21 changed files
with
3,919 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# Logs | ||
logs | ||
*.log | ||
npm-debug.log* | ||
|
||
# Runtime data | ||
pids | ||
*.pid | ||
*.seed | ||
|
||
# Directory for instrumented libs generated by jscoverage/JSCover | ||
lib-cov | ||
|
||
# Coverage directory used by tools like istanbul | ||
coverage | ||
.nyc_output | ||
coverage.* | ||
|
||
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) | ||
.grunt | ||
|
||
# node-waf configuration | ||
.lock-wscript | ||
|
||
# Compiled binary addons (http://nodejs.org/api/addons.html) | ||
build/Release | ||
|
||
# Dependency directory | ||
node_modules | ||
|
||
# Optional npm cache directory | ||
.npm | ||
|
||
# Optional REPL history | ||
.node_repl_history | ||
|
||
typings/ | ||
lib/*.js | ||
test/*.js | ||
*.map |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Matilda - Not working (work in progress) | ||
|
||
## Introduction | ||
|
||
> Open source Task management software for service based work. i.e. Plumbers, Steal work, etc | ||
## Code Samples | ||
|
||
> Coming soon | ||
## Installation | ||
|
||
> Coming soon |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"api": { | ||
"base": "api", | ||
"version": "v1" | ||
}, | ||
"logger": { | ||
"level": "dev" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './src/errors'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
import * as os from 'os'; | ||
|
||
export interface IErrorConstructor { | ||
new(msg: string): any; | ||
} | ||
|
||
export interface IErrorDefinition { | ||
statusCode: number; | ||
status: string; | ||
} | ||
|
||
export function declareError(statusCode: number, status: string): IErrorDefinition { | ||
return { statusCode, status }; | ||
} | ||
|
||
export const ERRORS = { | ||
EUNAUTHORIZED: declareError(401, 'E-UNAUTHORIZED'), | ||
ETOKENEXPIRED: declareError(401, 'E-TOKENEXPIRED'), | ||
ETOKENINVALID: declareError(401, 'E-TOKENINVALID'), | ||
EINVALID: declareError(400, 'E-INVALID'), | ||
ENOTFOUND: declareError(404, 'E-NOTFOUND'), | ||
EEXISTS: declareError(409, 'E-EXISTS'), | ||
ECONFLICT: declareError(500, 'E-CONFLICT'), | ||
EOTHER: declareError(500, 'E-OTHER'), | ||
EPIPECLOSED: declareError(0, 'E-PIPECLOSED'), | ||
EUNSUPPORTED: declareError(500, 'E-UNSU'), | ||
ETARGET: declareError(500, 'E-TARGET'), | ||
ETIMEOUT: declareError(408, 'E-TIMEOUT'), | ||
EBUSY: declareError(503, 'E-BUSY'), | ||
ERATELIMIT: declareError(429, 'E-RATELIMIT'), | ||
E2FAREQUIRED: declareError(401, 'E-2FAREQUIRED') | ||
}; | ||
|
||
export interface PlatformError { | ||
statusCode: number; | ||
status: string; | ||
message: string; | ||
source: string; | ||
reason: any; | ||
stack: string; | ||
$platformError: boolean; | ||
meta?: any; | ||
} | ||
|
||
function translateReasonIfNecessary(reason: any): any { | ||
if (!reason) { | ||
return reason; | ||
} | ||
if (reason.stack) { | ||
return { | ||
message: `${reason.name} : ${reason.message}`, | ||
stack: reason.stack | ||
}; | ||
} else if (typeof reason === 'string') { | ||
return reason; | ||
} | ||
return reason; | ||
} | ||
|
||
export function constructError(def: IErrorDefinition, message: string, reason?: any): PlatformError { | ||
let stack: string = (new Error()).stack || ''; | ||
let ret: PlatformError = { | ||
statusCode: def.statusCode, | ||
status: def.status, | ||
message, | ||
source: source(), | ||
reason: translateReasonIfNecessary(reason), | ||
stack, | ||
meta: {}, | ||
$platformError: true | ||
}; | ||
return ret; | ||
} | ||
|
||
export function throwError(def: IErrorDefinition, message: string, reason?: any): never { | ||
throw constructError(def, message, reason); | ||
} | ||
|
||
export function isError(def: IErrorDefinition, e: any): boolean { | ||
return e && e.status && e.status === def.status; | ||
} | ||
|
||
export function source() { | ||
let name = process.env['SERVICE_NAME'] || process.argv; | ||
let hostname = os.hostname(); | ||
let pid = process.pid; | ||
|
||
return `${hostname}:${pid}:${name}`; | ||
} | ||
|
||
export function convertToPlatformError(e: any) { | ||
if (e.$platformError) { | ||
return e; | ||
} | ||
if (e.constructor.name === 'ValidationError') { | ||
return constructError(ERRORS.EINVALID, e.message); | ||
} | ||
|
||
return constructError(ERRORS.ETARGET, `Error: ${e}`, e); | ||
} | ||
|
||
export function sanitizeError(e: any) { | ||
if (!e) { return e; } | ||
let r: any = {}; | ||
if (e.statusCode) { r.statusCode = e.statusCode; } | ||
if (e.status) { r.status = e.status; } | ||
if (e.message) { r.message = e.message; } | ||
if (e.reason) { r.reason = sanitizeError(e.reason); } | ||
if (e.meta) { r.meta = e.meta; } | ||
return r; | ||
} |
Oops, something went wrong.