Skip to content

Commit

Permalink
error message is now extracted using util.inspect (#7252)
Browse files Browse the repository at this point in the history
util.inspect used for new errors and "500" or unkown errors
this pretty prints the error nicely and we can use the new error cause
parameter to wrap builtin errors
  • Loading branch information
daneryl authored Sep 22, 2024
1 parent 127c449 commit 72e172e
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 20 deletions.
9 changes: 2 additions & 7 deletions app/api/csv/arrangeThesauri.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,12 @@ import { LabelInfoBase } from './typeParsers/select';
import { headerWithLanguage } from './csvDefinitions';

class ArrangeThesauriError extends Error {
source: Error;

row: CSVRow;

index: number;

constructor(source: Error, row: CSVRow, index: number) {
super(source.message);
this.source = source;
super(source.message, { cause: source });
this.row = row;
this.index = index;
}
Expand Down Expand Up @@ -163,9 +160,7 @@ const tryAddingLabel = (
const map = thesauriValueData[id];
if (isStandaloneGroup(map, labelInfo)) {
throw new Error(
`The label "${
labelInfo.label
}" at property "${name}" is a group label in line:\n${JSON.stringify(row)}`
`The label "${labelInfo.label}" at property "${name}" is a group label in line:\n${JSON.stringify(row)}`
);
}
const { childInfo, parentInfo } = pickParentChild(labelInfo);
Expand Down
7 changes: 2 additions & 5 deletions app/api/files/S3Storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,8 @@ import {
import { config } from 'api/config';

class S3TimeoutError extends Error {
readonly s3TimeoutError: Error;

constructor(s3TimeoutError: Error) {
super(s3TimeoutError.message);
this.s3TimeoutError = s3TimeoutError;
constructor(cause: Error) {
super(cause.message, { cause });
}
}

Expand Down
11 changes: 6 additions & 5 deletions app/api/utils/handleError.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { legacyLogger } from 'api/log';
import Ajv from 'ajv';
import { createError } from 'api/utils/index';
import { appContext } from 'api/utils/AppContext';
import { UnauthorizedError } from 'api/authorization.v2/errors/UnauthorizedError';
import { ValidationError } from 'api/common.v2/validation/ValidationError';
import { FileNotFound } from 'api/files/FileNotFound';
import { S3TimeoutError } from 'api/files/S3Storage';
import { legacyLogger } from 'api/log';
import { appContext } from 'api/utils/AppContext';
import { createError } from 'api/utils/index';
import util from 'node:util';

const ajvPrettifier = error => {
const errorMessage = [error.message];
Expand Down Expand Up @@ -60,11 +61,11 @@ const prettifyError = (error, { req = {}, uncaught = false } = {}) => {
let result = error;

if (error instanceof Error) {
result = { code: 500, message: error.stack, logLevel: 'error' };
result = { code: 500, message: util.inspect(error), logLevel: 'error' };
}

if (error instanceof S3TimeoutError) {
result = { code: 408, message: `${error.message}\n${error.stack}`, logLevel: 'debug' };
result = { code: 408, message: util.inspect(error), logLevel: 'debug' };
}

if (error instanceof Ajv.ValidationError) {
Expand Down
7 changes: 4 additions & 3 deletions app/api/utils/specs/handleError.spec.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { createError } from 'api/utils';
import { legacyLogger } from 'api/log';
import { createError } from 'api/utils';

import { errors as elasticErrors } from '@elastic/elasticsearch';
import { S3TimeoutError } from 'api/files/S3Storage';
import { appContext } from 'api/utils/AppContext';
import util from 'node:util';
import { handleError, prettifyError } from '../handleError';
import { S3TimeoutError } from 'api/files/S3Storage';

const contextRequestId = '1234';

Expand Down Expand Up @@ -48,7 +49,7 @@ describe('handleError', () => {
handleError(error);

expect(legacyLogger.error).toHaveBeenCalledWith(
`requestId: ${contextRequestId} \n${error.stack}
`requestId: ${contextRequestId} \n${util.inspect(error)}
original error: {
"name": "ConnectionError",
"meta": {
Expand Down

0 comments on commit 72e172e

Please sign in to comment.