Skip to content

Commit

Permalink
feat: Rework
Browse files Browse the repository at this point in the history
  • Loading branch information
jlenon7 committed Sep 17, 2021
1 parent 2f6eecb commit 3741e2b
Show file tree
Hide file tree
Showing 15 changed files with 145 additions and 61 deletions.
41 changes: 27 additions & 14 deletions .github/workflows/cd.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@

name: Exceptions Publish
name: CD Exceptions

on:
push:
Expand All @@ -12,15 +11,29 @@ jobs:
runs-on: ubuntu-18.04

steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: '13.x'
registry-url: 'https://npm.pkg.github.com'
- run: yarn
env:
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- run: yarn publish
env:
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
name: Deploy
- name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 0

- uses: actions/setup-node@v1
with:
node-version: '14.x'
registry-url: 'https://registry.npmjs.org'

- name: Download dependencies
run: npm install

- name: Transpile typescript to javascript
run: npm run build

- name: Automatic GitHub Release
uses: justincy/[email protected]
id: release

- name: Publish to NPM Registry
run: yarn publish --access public
if: steps.release.outputs.released == 'true'
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
name: Deploy
31 changes: 31 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: CI Exceptions

on:
pull_request:
branches:
- master

jobs:
build:

runs-on: ubuntu-18.04

steps:
- name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 0

- uses: actions/setup-node@v1
with:
node-version: '14.x'
registry-url: 'https://registry.npmjs.org'

- name: Download dependencies
run: npm install

- name: Verify project lint and try to fix it
run: npm run lint:fix

- name: Run the tests from project
run: npm run test
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class MyCustomException extends BaseException {
throw new MyCustomException({ error: 'object', use: 'as you want!' })
```

---

### SecJS Exceptions

> Or if you prefer, you can use the already built in exception from this package
Expand Down
26 changes: 22 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"build": "tsc",
"test": "jest --verbose",
"test:debug": "DEBUG=* jest --verbose",
"lint:fix": "eslint ./src --fix --ext=.ts"
"lint:fix": "eslint \"{src,tests}/**/*.ts\" --fix"
},
"dependencies": {},
"devDependencies": {
Expand Down Expand Up @@ -98,22 +98,40 @@
},
"extends": [
"standard",
"plugin:@typescript-eslint/recommended",
"plugin:prettier/recommended"
],
"globals": {
"use": true,
"Atomics": "readonly",
"SharedArrayBuffer": "readonly"
},
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": 2018
"ecmaVersion": 2018,
"sourceType": "module"
},
"plugins": [
"@typescript-eslint",
"prettier"
],
"rules": {
"dot-notation": "off",
"camelcase": "off",
"no-undef": "off",
"@typescript-eslint/no-var-requires": "off",
"no-useless-constructor": "off",
"@typescript-eslint/no-useless-constructor": "off",
"@typescript-eslint/camelcase": "off",
"@typescript-eslint/explicit-module-boundary-types": "off",
"@typescript-eslint/interface-name-prefix": "off",
"@typescript-eslint/no-explicit-any": "off",
"prettier/prettier": "error",
"camelcase": "off"
"@typescript-eslint/no-unused-vars": [
"error",
{
"argsIgnorePattern": "_"
}
]
}
},
"files": [
Expand Down
19 changes: 13 additions & 6 deletions src/BaseException.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
export interface ExceptionJSON {
name: string
status: number
content: string | any
isSecJsException: boolean
stack?: any
}

export abstract class BaseException extends Error {
private readonly _name: string
private readonly _status: number
private readonly _content: string | object
private readonly _content: string | any

protected constructor(name: string, content: string | object, status?: number) {
super(typeof content === 'string' ? content : name)
protected constructor(name: string, content: string | any, status?: number) {
super(typeof content === 'string' ? content : name)

this._name = name
this._status = status
Expand All @@ -23,12 +31,11 @@ export abstract class BaseException extends Error {
return this._status
}

// @ts-ignore
get content(): string | object {
get content(): string | any {
return this._content
}

toJSON(stack = true): object {
toJSON(stack = true): ExceptionJSON {
const response: any = {
name: this._name,
status: this._status,
Expand Down
6 changes: 3 additions & 3 deletions src/Exceptions/BadRequestException.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { BaseException } from "../BaseException";
import { BaseException } from '../BaseException'

export class BadRequestException extends BaseException {
constructor(content: string | object = 'Bad Request Error', code = 400) {
super(BadRequestException.name, content, code);
constructor(content: string | any = 'Bad Request Error', code = 400) {
super(BadRequestException.name, content, code)
}
}
6 changes: 3 additions & 3 deletions src/Exceptions/ForbiddenException.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { BaseException } from "../BaseException";
import { BaseException } from '../BaseException'

export class ForbiddenException extends BaseException {
constructor(content: string | object = 'Forbidden Error', code = 403) {
super(ForbiddenException.name, content, code);
constructor(content: string | any = 'Forbidden Error', code = 403) {
super(ForbiddenException.name, content, code)
}
}
6 changes: 3 additions & 3 deletions src/Exceptions/InternalServerException.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { BaseException } from "../BaseException";
import { BaseException } from '../BaseException'

export class InternalServerException extends BaseException {
constructor(content: string | object = 'Internal Server Error', code = 500) {
super(InternalServerException.name, content, code);
constructor(content: string | any = 'Internal Server Error', code = 500) {
super(InternalServerException.name, content, code)
}
}
6 changes: 3 additions & 3 deletions src/Exceptions/InvalidMethodException.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { BaseException } from "../BaseException";
import { BaseException } from '../BaseException'

export class InvalidMethodException extends BaseException {
constructor(content: string | object = 'Invalid Method Error', code = 405) {
super(InvalidMethodException.name, content, code);
constructor(content: string | any = 'Invalid Method Error', code = 405) {
super(InvalidMethodException.name, content, code)
}
}
6 changes: 3 additions & 3 deletions src/Exceptions/NotFoundException.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { BaseException } from "../BaseException";
import { BaseException } from '../BaseException'

export class NotFoundException extends BaseException {
constructor(content: string | object = 'Not Found Error', code = 404) {
super(NotFoundException.name, content, code);
constructor(content: string | any = 'Not Found Error', code = 404) {
super(NotFoundException.name, content, code)
}
}
6 changes: 3 additions & 3 deletions src/Exceptions/NotImplementedException.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { BaseException } from "../BaseException";
import { BaseException } from '../BaseException'

export class NotImplementedException extends BaseException {
constructor(content: string | object = 'Not Implemented Error', code = 501) {
super(NotImplementedException.name, content, code);
constructor(content: string | any = 'Not Implemented Error', code = 501) {
super(NotImplementedException.name, content, code)
}
}
6 changes: 3 additions & 3 deletions src/Exceptions/UnauthorizedException.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { BaseException } from "../BaseException";
import { BaseException } from '../BaseException'

export class UnauthorizedException extends BaseException {
constructor(content: string | object = 'Unauthorized Error', code = 401) {
super(UnauthorizedException.name, content, code);
constructor(content: string | any = 'Unauthorized Error', code = 401) {
super(UnauthorizedException.name, content, code)
}
}
9 changes: 6 additions & 3 deletions src/Exceptions/UnprocessableEntityException.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { BaseException } from "../BaseException";
import { BaseException } from '../BaseException'

export class UnprocessableEntityException extends BaseException {
constructor(content: string | object = 'Unprocessable Entity Error', code = 403) {
super(UnprocessableEntityException.name, content, code);
constructor(
content: string | any = 'Unprocessable Entity Error',
code = 403,
) {
super(UnprocessableEntityException.name, content, code)
}
}
6 changes: 3 additions & 3 deletions src/Exceptions/UnsupportedMediaException.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { BaseException } from "../BaseException";
import { BaseException } from '../BaseException'

export class UnsupportedMediaException extends BaseException {
constructor(content: string | object = 'Unsupported Media Error', code = 403) {
super(UnsupportedMediaException.name, content, code);
constructor(content: string | any = 'Unsupported Media Error', code = 403) {
super(UnsupportedMediaException.name, content, code)
}
}
30 changes: 20 additions & 10 deletions tests/exceptions.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ForbiddenException } from "../src/Exceptions/ForbiddenException";
import { BadRequestException } from "../src/Exceptions/BadRequestException";
import { BaseException } from "../src/BaseException";
import { ForbiddenException } from '../src/Exceptions/ForbiddenException'
import { BadRequestException } from '../src/Exceptions/BadRequestException'
import { BaseException } from '../src/BaseException'

describe('\n Exceptions', () => {
it('should be able to create BadRequestException', async () => {
Expand All @@ -14,7 +14,7 @@ describe('\n Exceptions', () => {
name: 'BadRequestException',
status: 400,
content: 'Bad Request Error',
isSecJsException: true
isSecJsException: true,
})
})

Expand All @@ -29,28 +29,38 @@ describe('\n Exceptions', () => {
name: 'ForbiddenException',
status: 403,
content: 'Forbidden Error',
isSecJsException: true
isSecJsException: true,
})
})

it('should be able to a new error class using BaseException', async () => {
class MyCustomException extends BaseException {
constructor(content: string | object = 'My default error', status = 415) {
super(`My custom name, use ${MyCustomException.name} here`, content, status);
constructor(content: string | any = 'My default error', status = 415) {
super(
`My custom name, use ${MyCustomException.name} here`,
content,
status,
)
}
}

const exception = new MyCustomException({ error: 'object', use: 'as you want!' })
const exception = new MyCustomException({
error: 'object',
use: 'as you want!',
})

expect(exception.status).toBe(415)
expect(exception.isSecJsException).toBe(true)
expect(exception.name).toBe('My custom name, use MyCustomException here')
expect(exception.content).toStrictEqual({ error: 'object', use: 'as you want!' })
expect(exception.content).toStrictEqual({
error: 'object',
use: 'as you want!',
})
expect(exception.toJSON(false)).toStrictEqual({
name: 'My custom name, use MyCustomException here',
status: 415,
content: { error: 'object', use: 'as you want!' },
isSecJsException: true
isSecJsException: true,
})
})
})

0 comments on commit 3741e2b

Please sign in to comment.