Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
unknown committed Dec 18, 2018
0 parents commit a5c251a
Show file tree
Hide file tree
Showing 21 changed files with 3,919 additions and 0 deletions.
40 changes: 40 additions & 0 deletions .gitignore
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
13 changes: 13 additions & 0 deletions README.md
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
9 changes: 9 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"api": {
"base": "api",
"version": "v1"
},
"logger": {
"level": "dev"
}
}
1 change: 1 addition & 0 deletions lib/common/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './src/errors';
111 changes: 111 additions & 0 deletions lib/common/src/errors.ts
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;
}
Loading

0 comments on commit a5c251a

Please sign in to comment.