Skip to content

@accounts/error : new AccountsError package with message formatting #171

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Mar 14, 2018
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions packages/error/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
__tests__
src/
coverage/
node_modules
tsconfig.json
.npmignore
yarn.lock
37 changes: 37 additions & 0 deletions packages/error/__tests__/accounts-error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import AccountsError from '../src';

describe('AccountsError', () => {
it('should return an Error', () => {
expect(new AccountsError() instanceof Error).toBe(true);
});

it('should be an instance of AccountsError', () => {
expect(new AccountsError() instanceof AccountsError).toBe(true);
});

it("should take 1 parameter and make it it's message", () => {
expect(new AccountsError('message').message).toBe('message');
});

it('should take 3 parameter and make a formatted Message', () => {
expect(new AccountsError('Package', 'method', 'reason').message).toBe('[ Accounts - Package ] method : reason');
});

it('should have a public property packageName', () => {
expect(new AccountsError('Package', 'method', 'reason').packageName).toBe('Package');
});

it('should have a public property functionName', () => {
expect(new AccountsError('Package', 'method', 'reason').functionName).toBe('method');
});

it('should have a public property reason', () => {
expect(new AccountsError('Package', 'method', 'reason').reason).toBe('reason');
});

Error.captureStackTrace = null;

it('should capture a stackTrace even if Error.captureStackTrace is not available', () => {
expect(new AccountsError().stack).not.toBe(undefined);
});
});
7 changes: 7 additions & 0 deletions packages/error/__tests__/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import AccountsError from '../src';

describe('AccountsError', () => {
it('should have a default export AccountsError', () => {
expect(typeof AccountsError).toBe('function');
});
});
51 changes: 51 additions & 0 deletions packages/error/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{
"name": "@accounts/error",
"version": "0.1.0-beta.3",
"description": "Accounts-js Error",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
"publishConfig": {
"access": "public"
},
"scripts": {
"clean": "rimraf lib",
"start": "tsc --watch",
"precompile": "yarn clean",
"compile": "tsc",
"prepublishOnly": "yarn compile",
"test": "yarn testonly",
"test-ci": "yarn lint && yarn coverage",
"testonly": "jest",
"test:watch": "jest --watch",
"coverage": "yarn testonly -- --coverage"
},
"jest": {
"transform": {
".(ts|tsx)": "<rootDir>/../../node_modules/ts-jest/preprocessor.js"
},
"testRegex": "(/__tests__/.*|\\.(test|spec))\\.(ts|tsx)$",
"moduleFileExtensions": [
"ts",
"js"
]
},
"repository": {
"type": "git",
"url": "https://github.com/accounts-js/accounts/tree/master/packages/error"
},
"keywords": [
"rest",
"graphql",
"grant",
"auth",
"authentication",
"accounts",
"users",
"oauth"
],
"author": "Elies Lou (Aetherall)",
"license": "MIT",
"devDependencies": {
"rimraf": "^2.6.2"
}
}
30 changes: 30 additions & 0 deletions packages/error/src/accounts-error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
export default class AccountsError extends Error {
public packageName?: string;

public functionName?: string;

public reason?: string;

constructor(packageName?: string, functionName?: string, reason?: string) {
// Build Error message from parameters
const message = reason ? `[ Accounts - ${packageName} ] ${functionName} : ${reason}` : packageName;

// Build the underlying Error
super(message);

// Assign parameters for future use
this.packageName = packageName;
this.functionName = functionName;
this.reason = reason;

// Set the prototype to AccountsError so "instanceof AccountsError" returns true
Object.setPrototypeOf(this, AccountsError.prototype);

// Recapture the stack trace to avoid this function to be in it
if (typeof Error.captureStackTrace === 'function') {
Error.captureStackTrace(this, this.constructor);
} else {
this.stack = new Error(message).stack;
}
}
}
1 change: 1 addition & 0 deletions packages/error/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './accounts-error';
15 changes: 15 additions & 0 deletions packages/error/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"extends": "../../tsconfig",
"compilerOptions": {
"rootDir": "./src",
"outDir": "./lib",
"typeRoots": [
"node_modules/@types"
]
},
"exclude": [
"node_modules",
"__tests__",
"lib"
]
}