(
req: http.IncomingMessage,
res: http.ServerResponse,
requestId: string,
errorCode?: string,
statusCode: number = 500,
headers: HttpHeadersConfig = {}
)
| 1348 | } |
| 1349 | |
| 1350 | async sendError( |
| 1351 | req: http.IncomingMessage, |
| 1352 | res: http.ServerResponse, |
| 1353 | requestId: string, |
| 1354 | errorCode?: string, |
| 1355 | statusCode: number = 500, |
| 1356 | headers: HttpHeadersConfig = {} |
| 1357 | ): Promise<void> { |
| 1358 | res.statusCode = statusCode; |
| 1359 | this.setResponseHeaders(res, requestId, headers); |
| 1360 | |
| 1361 | const http_status_description = generateHttpStatusDescription(statusCode); |
| 1362 | const error_code = errorCode || http_status_description; |
| 1363 | const errorMessage = generateErrorMessage(statusCode, error_code); |
| 1364 | |
| 1365 | let body: string; |
| 1366 | const { accept = 'text/plain' } = req.headers; |
| 1367 | if (accept.includes('json')) { |
| 1368 | res.setHeader('content-type', 'application/json'); |
| 1369 | const json = JSON.stringify({ |
| 1370 | error: { |
| 1371 | code: statusCode, |
| 1372 | message: errorMessage.title, |
| 1373 | }, |
| 1374 | }); |
| 1375 | body = `${json}\n`; |
| 1376 | } else if (accept.includes('html')) { |
| 1377 | res.setHeader('content-type', 'text/html; charset=utf-8'); |
| 1378 | |
| 1379 | let view: string; |
| 1380 | if (statusCode === 404) { |
| 1381 | view = errorTemplate404({ |
| 1382 | ...errorMessage, |
| 1383 | http_status_code: statusCode, |
| 1384 | http_status_description, |
| 1385 | error_code, |
| 1386 | request_id: requestId, |
| 1387 | }); |
| 1388 | } else if (statusCode === 502) { |
| 1389 | view = errorTemplate502({ |
| 1390 | ...errorMessage, |
| 1391 | http_status_code: statusCode, |
| 1392 | http_status_description, |
| 1393 | error_code, |
| 1394 | request_id: requestId, |
| 1395 | }); |
| 1396 | } else { |
| 1397 | view = errorTemplate({ |
| 1398 | http_status_code: statusCode, |
| 1399 | http_status_description, |
| 1400 | error_code, |
| 1401 | request_id: requestId, |
| 1402 | }); |
| 1403 | } |
| 1404 | body = errorTemplateBase({ |
| 1405 | http_status_code: statusCode, |
| 1406 | http_status_description, |
| 1407 | view, |
no test coverage detected