@sn1006/common 1.0.9 → 1.0.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,9 +1,9 @@
1
- import CustomError from './custom-error';
2
- export declare class BadRequestError extends CustomError {
1
+ import { NormalizedError } from './normalized-error';
2
+ export declare class BadRequestError extends NormalizedError {
3
3
  message: string;
4
4
  status: number;
5
5
  constructor(message: string);
6
- serializeError(): {
6
+ serializeErrors(): {
7
7
  message: string;
8
8
  }[];
9
9
  }
@@ -1,18 +1,15 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
2
  Object.defineProperty(exports, "__esModule", { value: true });
6
3
  exports.BadRequestError = void 0;
7
- const custom_error_1 = __importDefault(require("./custom-error"));
8
- class BadRequestError extends custom_error_1.default {
4
+ const normalized_error_1 = require("./normalized-error");
5
+ class BadRequestError extends normalized_error_1.NormalizedError {
9
6
  constructor(message) {
10
7
  super(message);
11
8
  this.message = message;
12
9
  this.status = 400;
13
10
  Object.setPrototypeOf(this, BadRequestError.prototype);
14
11
  }
15
- serializeError() {
12
+ serializeErrors() {
16
13
  return [{ message: this.message }];
17
14
  }
18
15
  }
@@ -1,8 +1,8 @@
1
- import CustomError from './custom-error';
2
- export default class DBError extends CustomError {
1
+ import { NormalizedError } from './normalized-error';
2
+ export default class DBError extends NormalizedError {
3
3
  status: number;
4
4
  constructor();
5
- serializeError(): {
5
+ serializeErrors(): {
6
6
  message: string;
7
7
  field?: string | undefined;
8
8
  }[];
@@ -1,16 +1,13 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
2
  Object.defineProperty(exports, "__esModule", { value: true });
6
- const custom_error_1 = __importDefault(require("./custom-error"));
7
- class DBError extends custom_error_1.default {
3
+ const normalized_error_1 = require("./normalized-error");
4
+ class DBError extends normalized_error_1.NormalizedError {
8
5
  constructor() {
9
6
  super('Error connecting to DB');
10
7
  this.status = 500;
11
8
  Object.setPrototypeOf(this, DBError.prototype);
12
9
  }
13
- serializeError() {
10
+ serializeErrors() {
14
11
  return [{ message: this.message }];
15
12
  }
16
13
  }
@@ -0,0 +1,9 @@
1
+ export declare abstract class NormalizedError extends Error {
2
+ abstract status: number;
3
+ constructor(message: string);
4
+ abstract serializeErrors(): SerializedError[];
5
+ }
6
+ export interface SerializedError {
7
+ message: string;
8
+ field?: string;
9
+ }
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.NormalizedError = void 0;
4
+ // the purpose of this class is to "normalize" errors between services
5
+ class NormalizedError extends Error {
6
+ constructor(message) {
7
+ super(message);
8
+ Object.setPrototypeOf(this, NormalizedError.prototype);
9
+ }
10
+ }
11
+ exports.NormalizedError = NormalizedError;
@@ -1,8 +1,8 @@
1
- import CustomError from './custom-error';
2
- export declare class NotAuthorizedError extends CustomError {
1
+ import { NormalizedError } from './normalized-error';
2
+ export declare class NotAuthorizedError extends NormalizedError {
3
3
  status: number;
4
4
  constructor();
5
- serializeError(): {
5
+ serializeErrors(): {
6
6
  message: string;
7
7
  }[];
8
8
  }
@@ -1,17 +1,14 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
2
  Object.defineProperty(exports, "__esModule", { value: true });
6
3
  exports.NotAuthorizedError = void 0;
7
- const custom_error_1 = __importDefault(require("./custom-error"));
8
- class NotAuthorizedError extends custom_error_1.default {
4
+ const normalized_error_1 = require("./normalized-error");
5
+ class NotAuthorizedError extends normalized_error_1.NormalizedError {
9
6
  constructor() {
10
7
  super('Not authorized');
11
8
  this.status = 401;
12
9
  Object.setPrototypeOf(this, NotAuthorizedError.prototype);
13
10
  }
14
- serializeError() {
11
+ serializeErrors() {
15
12
  return [{ message: this.message }];
16
13
  }
17
14
  }
@@ -1,8 +1,8 @@
1
- import CustomError from './custom-error';
2
- export declare class NotFoundError extends CustomError {
1
+ import { NormalizedError } from './normalized-error';
2
+ export declare class NotFoundError extends NormalizedError {
3
3
  status: number;
4
4
  constructor();
5
- serializeError(): {
5
+ serializeErrors(): {
6
6
  message: string;
7
7
  }[];
8
8
  }
@@ -1,17 +1,14 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
2
  Object.defineProperty(exports, "__esModule", { value: true });
6
3
  exports.NotFoundError = void 0;
7
- const custom_error_1 = __importDefault(require("./custom-error"));
8
- class NotFoundError extends custom_error_1.default {
4
+ const normalized_error_1 = require("./normalized-error");
5
+ class NotFoundError extends normalized_error_1.NormalizedError {
9
6
  constructor() {
10
7
  super("Resource not found");
11
8
  this.status = 404;
12
9
  Object.setPrototypeOf(this, NotFoundError.prototype);
13
10
  }
14
- serializeError() {
11
+ serializeErrors() {
15
12
  return [{ message: this.message }];
16
13
  }
17
14
  }
@@ -1,10 +1,10 @@
1
- import CustomError from './custom-error';
1
+ import { NormalizedError } from './normalized-error';
2
2
  import { ValidationError } from 'express-validator';
3
- declare class CustomValidationError extends CustomError {
3
+ declare class CustomValidationError extends NormalizedError {
4
4
  errors: ValidationError[];
5
5
  status: number;
6
6
  constructor(errors: ValidationError[]);
7
- serializeError(): {
7
+ serializeErrors(): {
8
8
  message: any;
9
9
  field: string;
10
10
  }[];
@@ -1,17 +1,14 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
2
  Object.defineProperty(exports, "__esModule", { value: true });
6
- const custom_error_1 = __importDefault(require("./custom-error"));
7
- class CustomValidationError extends custom_error_1.default {
3
+ const normalized_error_1 = require("./normalized-error");
4
+ class CustomValidationError extends normalized_error_1.NormalizedError {
8
5
  constructor(errors) {
9
- super("Invalid username or password");
6
+ super("Validaton error");
10
7
  this.errors = errors;
11
8
  this.status = 400;
12
9
  Object.setPrototypeOf(this, CustomValidationError.prototype);
13
10
  }
14
- serializeError() {
11
+ serializeErrors() {
15
12
  return this.errors.map(err => {
16
13
  const ferr = err;
17
14
  return { message: ferr.msg, field: ferr.path };
package/build/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  export * from './errors/bad-request-error';
2
- export * from './errors/custom-error';
2
+ export * from './errors/normalized-error';
3
3
  export * from './errors/db-error';
4
4
  export * from './errors/validation-error';
5
5
  export * from './errors/not-authorized-error';
package/build/index.js CHANGED
@@ -15,7 +15,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./errors/bad-request-error"), exports);
18
- __exportStar(require("./errors/custom-error"), exports);
18
+ __exportStar(require("./errors/normalized-error"), exports);
19
19
  __exportStar(require("./errors/db-error"), exports);
20
20
  __exportStar(require("./errors/validation-error"), exports);
21
21
  __exportStar(require("./errors/not-authorized-error"), exports);
@@ -1,14 +1,11 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
2
  Object.defineProperty(exports, "__esModule", { value: true });
6
3
  exports.errorHandler = void 0;
7
- const custom_error_1 = __importDefault(require("../errors/custom-error"));
4
+ const normalized_error_1 = require("../errors/normalized-error");
8
5
  const errorHandler = (err, req, res, next) => {
9
6
  console.log(err);
10
- if (err instanceof custom_error_1.default) {
11
- return res.status(err.status).send(err.serializeError());
7
+ if (err instanceof normalized_error_1.NormalizedError) {
8
+ return res.status(err.status).send(err.serializeErrors());
12
9
  }
13
10
  return res.status(400).send('Something went wrong');
14
11
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sn1006/common",
3
- "version": "1.0.9",
3
+ "version": "1.0.12",
4
4
  "description": "",
5
5
  "main": "./build/index.js",
6
6
  "types": "./build/index.d.ts",
@@ -1,9 +0,0 @@
1
- declare abstract class CustomError extends Error {
2
- abstract status: number;
3
- constructor(message: string);
4
- abstract serializeError(): {
5
- message: string;
6
- field?: string;
7
- }[];
8
- }
9
- export default CustomError;
@@ -1,9 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- class CustomError extends Error {
4
- constructor(message) {
5
- super(message);
6
- Object.setPrototypeOf(this, CustomError.prototype);
7
- }
8
- }
9
- exports.default = CustomError;