Skip to content

LW-9018 fix unit tests #979

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions packages/util/src/errors.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { CustomError } from 'ts-custom-error';
import { toSerializableObject } from './serializableObject';

export const formatErrorMessage = (reason: string, detail?: string) => reason + (detail ? ` (${detail})` : '');

interface ErrorLike {
message: string;
stack: string;
data?: unknown;
}

interface WithInnerError {
Expand Down Expand Up @@ -66,10 +64,6 @@ export class ComposableError<InnerError = unknown> extends CustomError {
[firstLineOfInnerErrorStack, ...innerErrorStack] = innerError.stack.split(ComposableError.stackDelimiter);

message = `${message} due to\n ${firstLineOfInnerErrorStack}`;

if (innerError.data) {
innerError.data = toSerializableObject(innerError.data);
}
}

if (typeof innerError === 'string') message = `${message} due to\n ${innerError}`;
Expand Down
10 changes: 10 additions & 0 deletions packages/util/src/serializableObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ const defaultGetErrorPrototype: GetErrorPrototype = () => Error.prototype;
const defaultTransformKey: TransformKey = (key) => key;
const defaultTransformationTypeKey = '__type';

const hasInnerError = (error: unknown): error is { innerError: unknown } =>
error !== null && typeof error === 'object' && 'innerError' in (error as never);

const innerErrorHasData = (innerError: unknown): innerError is { data: unknown } =>
typeof innerError === 'object' && innerError !== null && 'data' in innerError;

export const toSerializableObject = (obj: unknown, options: ToSerializableObjectOptions = {}): unknown => {
if (PLAIN_TYPES.has(typeof obj)) return obj;
const { transformationTypeKey = defaultTransformationTypeKey, serializeKey = defaultTransformKey } = options;
Expand All @@ -41,7 +47,11 @@ export const toSerializableObject = (obj: unknown, options: ToSerializableObject
const value = Buffer.from(arr).toString('hex');
return { [transformationTypeKey]: 'Buffer', value };
}

if (obj instanceof Error) {
if (hasInnerError(obj) && innerErrorHasData(obj.innerError)) {
obj.innerError.data = toSerializableObject(obj.innerError.data);
}
return {
[transformationTypeKey]: 'Error',
value: serializeError(obj)
Expand Down
23 changes: 23 additions & 0 deletions packages/util/test/serializableObject.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import { CustomError } from 'ts-custom-error';
import { FromSerializableObjectOptions, fromSerializableObject, toSerializableObject } from '../src';

export class ErrorWithData extends CustomError {
innerError: { data: unknown };

constructor(data: unknown, message: string) {
super(message);
this.innerError = { data };
}
}

const serializeAndDeserialize = (obj: unknown, deserializeOptions?: FromSerializableObjectOptions) =>
fromSerializableObject(JSON.parse(JSON.stringify(toSerializableObject(obj))), deserializeOptions);

Expand Down Expand Up @@ -49,6 +58,20 @@ describe('serializableObject', () => {
expect(deserialized).toBeInstanceOf(CustomError);
});

it('supports error types with string data', () => {
const err = new ErrorWithData('some-data', 'msg');
const deserialized = serializeAndDeserialize(err, { getErrorPrototype: () => CustomError.prototype });
expect(deserialized).toEqual(err);
expect(deserialized).toBeInstanceOf(CustomError);
});

it('supports error types with object data', () => {
const err = new ErrorWithData({ bigIntProp: 15n }, 'msg');
const deserialized = serializeAndDeserialize(err, { getErrorPrototype: () => CustomError.prototype });
expect(deserialized).toEqual(err);
expect(deserialized).toBeInstanceOf(CustomError);
});

it('supports custom transformation discriminator key', () => {
const customKeyOption = { transformationTypeKey: 'discriminator' };
const obj = { bigint: 1n };
Expand Down
3 changes: 1 addition & 2 deletions packages/wallet/test/services/WalletUtil.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,7 @@ describe('WalletUtil', () => {
tx.body.certificates = [
{
__typename: Cardano.CertificateType.Registration,
// using foreign intentionally because registration is not signed so it should be accepted
stakeKeyHash: foreignRewardAccountHash
stakeKeyHash: mocks.stakeKeyHash
} as Cardano.NewStakeAddressCertificate,
{
__typename: Cardano.CertificateType.Unregistration,
Expand Down