@devmoods/express-extras 0.17.1 → 0.17.2

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.
package/dist/errors.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import * as Sentry from '@sentry/node';
2
2
  import { errors } from 'compose-middleware';
3
3
  import { STATUS_CODES } from 'node:http';
4
+ import { IntegrityError } from './postgres.js';
4
5
  export class ApiError extends Error {
5
6
  }
6
7
  export const BadRequest = (reason) => makeError(400, 'Invalid Params', { reason });
@@ -36,6 +37,12 @@ export function mapToProblem(error) {
36
37
  status: 500,
37
38
  };
38
39
  }
40
+ function convertToAPIError(error) {
41
+ if (error instanceof IntegrityError) {
42
+ return Conflict(error.message);
43
+ }
44
+ return error;
45
+ }
39
46
  function _errorHandler({ onError }) {
40
47
  return (err, req, res, next // eslint-disable-line
41
48
  ) => {
@@ -43,7 +50,8 @@ function _errorHandler({ onError }) {
43
50
  err = makeError(500);
44
51
  }
45
52
  onError?.(err);
46
- const problem = mapToProblem(err);
53
+ const apiError = convertToAPIError(err);
54
+ const problem = mapToProblem(apiError);
47
55
  res.status(problem.status).json(problem);
48
56
  };
49
57
  }
@@ -9,6 +9,9 @@ export declare class DoesNotExistError extends Error {
9
9
  export declare class MultipleRowsReturnedError extends Error {
10
10
  constructor(message: string);
11
11
  }
12
+ export declare class IntegrityError extends Error {
13
+ constructor(message: string);
14
+ }
12
15
  type Value = null | string | number | Date;
13
16
  export declare class Connection {
14
17
  client: PoolClient;
package/dist/postgres.js CHANGED
@@ -12,12 +12,26 @@ export class MultipleRowsReturnedError extends Error {
12
12
  this.name = 'MultipleRowsReturnedError';
13
13
  }
14
14
  }
15
+ export class IntegrityError extends Error {
16
+ constructor(message) {
17
+ super(message);
18
+ this.name = 'IntegrityError';
19
+ }
20
+ }
15
21
  export class Connection {
16
22
  constructor(client) {
17
23
  this.client = client;
18
24
  }
19
25
  async query(text, values) {
20
- return this.client.query(text, values);
26
+ try {
27
+ return await this.client.query(text, values);
28
+ }
29
+ catch (error) {
30
+ if (error.code && error.code === '23505') {
31
+ throw new IntegrityError(`${error.message}: ${error.detail}`);
32
+ }
33
+ throw error;
34
+ }
21
35
  }
22
36
  async all(text, values) {
23
37
  const result = await this.client.query(text, values);
@@ -27,7 +41,7 @@ export class Connection {
27
41
  return [];
28
42
  }
29
43
  async insert(text, values) {
30
- return await this.client.query(text, values);
44
+ return await this.query(text, values);
31
45
  }
32
46
  async first(text, values) {
33
47
  const result = await this.client.query(text, values);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@devmoods/express-extras",
3
- "version": "0.17.1",
3
+ "version": "0.17.2",
4
4
  "description": "Kubernetes-ready utilities for rapid Node.js application development",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -37,8 +37,8 @@
37
37
  "@devmoods/eslint-config": "^1.1.0",
38
38
  "@types/node": "20.2.5",
39
39
  "@types/supertest": "2.0.12",
40
- "@typescript-eslint/eslint-plugin": "^5.59.7",
41
- "@typescript-eslint/parser": "^5.59.7",
40
+ "@typescript-eslint/eslint-plugin": "^5.59.8",
41
+ "@typescript-eslint/parser": "^5.59.8",
42
42
  "chalk": "^5.2.0",
43
43
  "eslint": "^8.41.0",
44
44
  "eslint-plugin-jsx-a11y": "^6.7.1",
@@ -52,7 +52,7 @@
52
52
  "supertest": "6.3.3",
53
53
  "ts-node": "10.9.1",
54
54
  "typescript": "5.0.4",
55
- "vitest": "^0.31.1"
55
+ "vitest": "^0.31.2"
56
56
  },
57
57
  "dependencies": {
58
58
  "@devmoods/fetch": "^3.3.0",
@@ -74,7 +74,7 @@
74
74
  "cls-hooked": "^4.2.2",
75
75
  "compose-middleware": "^5.0.1",
76
76
  "cookie-parser": "^1.4.6",
77
- "dotenv": "^16.0.3",
77
+ "dotenv": "^16.1.0",
78
78
  "express": "^4.18.2",
79
79
  "helmet": "^7.0.0",
80
80
  "jsonwebtoken": "^9.0.0",