Skip to content

Commit

Permalink
fix: Add missing extensions error field to errorToPlainJson (#6029)
Browse files Browse the repository at this point in the history
* fix: Add missing extensions error field to errorToPlainJson

* ADD test for errorToPlainJson
  • Loading branch information
AleksandrMatuka committed May 23, 2024
1 parent 5c75df9 commit fdf9e19
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/plugins/utils/utils-error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export function errorToPlainJson(err: Error | TypeError | RxError | RxTypeError)
message: err.message,
rxdb: (err as any).rxdb,
parameters: (err as RxError).parameters,
extensions: (err as any).extensions,
code: (err as RxError).code,
/**
* stack must be last to make it easier to read the json in a console.
Expand Down
1 change: 1 addition & 0 deletions src/types/rx-error.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ export type PlainJsonError = {
message: string;
rxdb?: true;
code?: RxErrorKey;
extensions?: Record<string, any>;
parameters?: RxErrorParameters;
stack?: string;
};
Expand Down
26 changes: 25 additions & 1 deletion test/unit/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ import {
toWithDeleted,
stringToArrayBuffer,
arrayBufferToString,
clone
clone,
errorToPlainJson
} from '../../plugins/core/index.mjs';
import config from './config.ts';

Expand Down Expand Up @@ -504,5 +505,28 @@ describe('util.test.js', () => {
assert.strictEqual(str, back);
});
});
describe('.errorToPlainJson()', () => {
it('should return the correct result for an error containing all possible fields', () => {
const customError = {
name: 'CustomError',
message: 'This is a custom error',
rxdb: false,
extensions: { code: 'CUSTOM_ERR_CODE' },
parameters: { value: 'value' },
code: 'CUSTOM_ERR_CODE',
stack: 'CustomError: This is a custom error\n at someFile.js'
};

const result = errorToPlainJson(customError);

assert.strictEqual(result.name, customError.name);
assert.strictEqual(result.message, customError.message);
assert.strictEqual(result.rxdb, customError.rxdb);
assert.deepStrictEqual(result.extensions, customError.extensions);
assert.deepStrictEqual(result.parameters, customError.parameters);
assert.strictEqual(result.code, customError.code);
assert.strictEqual(result.stack, 'CustomError: This is a custom error \n at someFile.js');
});
});
});

0 comments on commit fdf9e19

Please sign in to comment.