Skip to content

Commit 808602a

Browse files
author
roman.vasilev
committed
chore: TSLint fixes
1 parent 75d10b4 commit 808602a

File tree

8 files changed

+43
-26
lines changed

8 files changed

+43
-26
lines changed

.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ module.exports = {
2626
'no-unused-vars': 0,
2727
'indent': 0,
2828
'unicorn/import-index': 0,
29+
'no-dupe-class-members': 0,
2930
'import/newline-after-import': 0,
3031
'import/no-duplicates': 1,
3132
'import/max-dependencies': [1, { 'max': 10 }],

src/custom-error.ts

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
export type CustomErrorOptions = { [K in Exclude<keyof CustomError, 'stack' | 'name'>]?: CustomError[K] };
22

3+
/**
4+
* Base class for custom errors
5+
*/
36
export class CustomError extends Error {
47

58
readonly status: number;
@@ -9,34 +12,28 @@ export class CustomError extends Error {
912

1013
constructor(options: CustomErrorOptions);
1114
constructor(...args: any[]);
12-
constructor(...args: any[]) {
15+
constructor(...args: any[]) { // tslint:disable-line:cognitive-complexity
1316
let message: string | undefined;
1417
let status: number = 0;
1518
let data: any;
1619
let code: string | undefined;
1720
let inner: Error | undefined;
1821
for (const arg of args) {
1922
const typeofArg = typeof arg;
20-
switch (true) {
21-
case message !== undefined && typeofArg === 'string': {
22-
code = arg;
23-
} break;
24-
case typeofArg === 'string': {
25-
message = arg;
26-
} break;
27-
case typeofArg === 'number': {
28-
status = arg;
29-
} break;
30-
case arg instanceof Error: {
31-
inner = arg;
32-
} break;
33-
case typeofArg === 'object': {
34-
if ('status' in arg) { status = arg.status; }
35-
if ('message' in arg) { message = arg.message; }
36-
if ('data' in arg) { data = arg.data; }
37-
if ('code' in arg) { code = arg.code; }
38-
if ('inner' in arg) { inner = arg.inner; }
39-
} break;
23+
if (message !== undefined && typeofArg === 'string') {
24+
code = arg;
25+
} else if (typeofArg === 'string') {
26+
message = arg;
27+
} else if (typeofArg === 'number') {
28+
status = arg;
29+
} else if (arg instanceof Error) {
30+
inner = arg;
31+
} else if (typeofArg === 'object') {
32+
if ('status' in arg) { status = arg.status; }
33+
if ('message' in arg) { message = arg.message; }
34+
if ('data' in arg) { data = arg.data; }
35+
if ('code' in arg) { code = arg.code; }
36+
if ('inner' in arg) { inner = arg.inner; }
4037
}
4138
}
4239
super(message);

src/exception-error.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { CustomError } from './custom-error';
22

3+
/**
4+
* General unknown error.
5+
*/
36
export class ExceptionError extends CustomError {
47

58
constructor(...args: any[]);

src/forbidden-error.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import { CustomError, CustomErrorOptions } from './custom-error';
22

3+
/**
4+
* Forbidden error, defaults:
5+
* status: 403
6+
* message: You don't have permission to do that.
7+
*/
38
export class ForbiddenError extends CustomError {
49

510
constructor(resource: string);
@@ -10,11 +15,11 @@ export class ForbiddenError extends CustomError {
1015
const [resource] = args;
1116
data = { resource };
1217
}
13-
args.unshift({
18+
args.unshift(<CustomErrorOptions>{
1419
data,
1520
status: 403,
1621
message: `You don't have permission to do that.`,
17-
} as CustomErrorOptions);
18-
super(...args as []);
22+
});
23+
super(...args);
1924
}
2025
}

src/index.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable tslint/config */
12
import { ExceptionError } from './exception-error';
23
import { ForbiddenError } from './forbidden-error';
34
import { NotFoundError } from './notfound-error';

src/notfound-error.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import { CustomError } from './custom-error';
22

3+
/**
4+
* Not found error, defaults:
5+
* status: 404
6+
*/
37
export class NotFoundError extends CustomError {
48

59
constructor(...args: any[]);
610
constructor(...args: any[]) {
7-
super(404, ...args as []);
11+
super(404, ...args);
812
}
913
}

src/validation-error.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import { CustomErrorOptions, CustomError } from './custom-error';
22

3+
/**
4+
* Validation error, defaults:
5+
* status: 400
6+
*/
37
export class ValidationError extends CustomError {
48

59
constructor(options: CustomErrorOptions);

tslint.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
"interface-name": false,
3535
"ban-types": false,
3636
"no-relative-imports": false,
37-
"missing-jsdoc": false
37+
"missing-jsdoc": false,
38+
"export-name": false,
39+
"no-any": false
3840
}
3941
}

0 commit comments

Comments
 (0)