@jpbs/common 1.1.6 → 1.1.8

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.
@@ -8,6 +8,7 @@ class BadRequestError extends custom_error_1.CustomError {
8
8
  super(message);
9
9
  this.message = message;
10
10
  this.statusCode = http_status_codes_1.StatusCodes.BAD_REQUEST;
11
+ Object.setPrototypeOf(this, BadRequestError.prototype);
11
12
  }
12
13
  serializeErrors() {
13
14
  return [{ message: this.message }];
@@ -4,7 +4,7 @@ exports.CustomError = void 0;
4
4
  class CustomError extends Error {
5
5
  constructor(message) {
6
6
  super(message);
7
- Object.setPrototypeOf(this, new.target.prototype);
7
+ Object.setPrototypeOf(this, CustomError.prototype);
8
8
  }
9
9
  }
10
10
  exports.CustomError = CustomError;
@@ -7,6 +7,7 @@ class EmailError extends custom_error_1.CustomError {
7
7
  constructor(message) {
8
8
  super(message);
9
9
  this.statusCode = http_status_codes_1.StatusCodes.INTERNAL_SERVER_ERROR;
10
+ Object.setPrototypeOf(this, EmailError.prototype);
10
11
  }
11
12
  serializeErrors() {
12
13
  return [{ message: this.message }];
@@ -8,6 +8,7 @@ class NotAuthorizedError extends custom_error_1.CustomError {
8
8
  super(message);
9
9
  this.message = message;
10
10
  this.statusCode = http_status_codes_1.StatusCodes.UNAUTHORIZED;
11
+ Object.setPrototypeOf(this, NotAuthorizedError.prototype);
11
12
  }
12
13
  serializeErrors() {
13
14
  return [{ message: this.message }];
@@ -8,6 +8,7 @@ class NotFoundError extends custom_error_1.CustomError {
8
8
  super(message);
9
9
  this.message = message;
10
10
  this.statusCode = http_status_codes_1.StatusCodes.NOT_FOUND;
11
+ Object.setPrototypeOf(this, NotFoundError.prototype);
11
12
  }
12
13
  serializeErrors() {
13
14
  return [{ message: this.message }];
@@ -8,6 +8,7 @@ class RequestValidationError extends custom_error_1.CustomError {
8
8
  super('Invalid request parameters');
9
9
  this.errors = errors;
10
10
  this.statusCode = http_status_codes_1.StatusCodes.BAD_REQUEST;
11
+ Object.setPrototypeOf(this, RequestValidationError.prototype);
11
12
  }
12
13
  serializeErrors() {
13
14
  return this.errors.map(err => {
@@ -5,14 +5,18 @@ const axios_1 = require("axios");
5
5
  const custom_error_1 = require("../errors/custom-error");
6
6
  const http_status_codes_1 = require("http-status-codes");
7
7
  const errorHandler = (err, _req, res, _next) => {
8
- var _a, _b;
8
+ var _a;
9
9
  if (err instanceof custom_error_1.CustomError) {
10
10
  res.status(err.statusCode).send({ errors: err.serializeErrors() });
11
11
  return;
12
12
  }
13
13
  if (err instanceof axios_1.AxiosError) {
14
- const message = ((_b = (_a = err.response) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.message) || 'An error occurred with the external service';
15
- res.status(http_status_codes_1.StatusCodes.BAD_GATEWAY).send({ errors: [{ message }] });
14
+ if (err.response && err.response.data && err.response.data.errors) {
15
+ const message = ((_a = err.response.data.errors[0]) === null || _a === void 0 ? void 0 : _a.message) || 'An error';
16
+ res.status(http_status_codes_1.StatusCodes.BAD_GATEWAY).send({ errors: [{ message }] });
17
+ return;
18
+ }
19
+ res.status(http_status_codes_1.StatusCodes.BAD_GATEWAY).send({ errors: [{ message: 'An error occurred with the external service' }] });
16
20
  return;
17
21
  }
18
22
  res.status(http_status_codes_1.StatusCodes.BAD_REQUEST).send({ errors: [{ message: 'Something went wrong' }] });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jpbs/common",
3
- "version": "1.1.6",
3
+ "version": "1.1.8",
4
4
  "main": "./build/index.js",
5
5
  "types": "./build/index.d.ts",
6
6
  "files": [