|
| 1 | +import { Prisma } from '@prisma/client'; |
1 | 2 | import compression from 'compression'; |
2 | 3 | import config from 'config'; |
3 | 4 | import cors from 'cors'; |
| 5 | +import { randomBytes } from 'crypto'; |
4 | 6 | import express from 'express'; |
5 | 7 | import helmet from 'helmet'; |
6 | 8 | import { join } from 'path'; |
7 | 9 | import querystring from 'querystring'; |
8 | | -import { randomBytes } from 'crypto'; |
9 | 10 |
|
10 | 11 | import { version as appVersion } from './package.json'; |
11 | 12 | import { getLogger, httpLogger } from './src/components/log'; |
@@ -124,11 +125,34 @@ export function errorHandler( |
124 | 125 | res: Response, |
125 | 126 | _next: NextFunction // eslint-disable-line @typescript-eslint/no-unused-vars |
126 | 127 | ): void { |
127 | | - if (err.stack) log.error(err); |
128 | | - |
129 | 128 | if (err instanceof Problem) { |
130 | 129 | err.send(req, res); |
| 130 | + } else if (err instanceof Prisma.PrismaClientKnownRequestError) { |
| 131 | + switch (err.code) { |
| 132 | + case 'P2003': |
| 133 | + new Problem(500, { |
| 134 | + type: err.code, |
| 135 | + title: err.meta?.constraint as string, |
| 136 | + detail: err.meta?.modelName as string |
| 137 | + }).send(req, res); |
| 138 | + break; |
| 139 | + case 'P2025': |
| 140 | + new Problem(404, { |
| 141 | + type: err.code, |
| 142 | + title: err.meta?.cause as string, |
| 143 | + detail: err.meta?.modelName as string |
| 144 | + }).send(req, res); |
| 145 | + break; |
| 146 | + default: |
| 147 | + new Problem(500, { |
| 148 | + type: err.code, |
| 149 | + title: err.meta?.cause as string, |
| 150 | + detail: err.meta?.modelName as string |
| 151 | + }).send(req, res); |
| 152 | + break; |
| 153 | + } |
131 | 154 | } else { |
| 155 | + if (err.stack) log.error(err); |
132 | 156 | new Problem(500, { detail: err.message ?? err.toString() }).send(req, res); |
133 | 157 | } |
134 | 158 | } |
|
0 commit comments