Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
gnarea committed Jun 13, 2022
1 parent 5422c2d commit a4cac71
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 33 deletions.
43 changes: 17 additions & 26 deletions src/lib/PoWebClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -397,44 +397,44 @@ describe('deliverParcel', () => {
test('HTTP 422 should throw a RefusedParcelError', async () => {
mockAxios.onPost('/parcels').reply(422, null);

const error = await getRejection(client.deliverParcel(parcelSerialized, signer));

expect(error).toBeInstanceOf(RefusedParcelError);
expect(error.message).toEqual('Parcel was rejected');
await expect(client.deliverParcel(parcelSerialized, signer)).rejects.toThrowWithMessage(
RefusedParcelError,
'Parcel was rejected',
);
});

test('RefusedParcelError should include rejection reason if available', async () => {
const message = 'Not enough postage';
mockAxios.onPost('/parcels').reply(422, { message });

const error = await getRejection(client.deliverParcel(parcelSerialized, signer));

expect(error).toBeInstanceOf(RefusedParcelError);
expect(error.message).toEqual(`Parcel was rejected: ${message}`);
await expect(client.deliverParcel(parcelSerialized, signer)).rejects.toThrowWithMessage(
RefusedParcelError,
`Parcel was rejected: ${message}`,
);
});

test('HTTP 50X should throw a ServerError', async () => {
mockAxios.onPost('/parcels').reply(500, null);

const error = await getRejection(client.deliverParcel(parcelSerialized, signer));

expect(error).toBeInstanceOf(ServerError);
expect(error.message).toEqual('Server was unable to get parcel (HTTP 500)');
await expect(client.deliverParcel(parcelSerialized, signer)).rejects.toThrowWithMessage(
ServerError,
'Server was unable to get parcel (HTTP 500)',
);
});

test('HTTP responses other than 20X/422/50X should throw errors', async () => {
mockAxios.onPost('/parcels').reply(400, null);

const error = await getRejection(client.deliverParcel(parcelSerialized, signer));

expect(error).toBeInstanceOf(ParcelDeliveryError);
expect(error.message).toEqual('Could not deliver parcel (HTTP 400)');
await expect(client.deliverParcel(parcelSerialized, signer)).rejects.toThrowWithMessage(
ParcelDeliveryError,
'Could not deliver parcel (HTTP 400)',
);
});

test('Other client exceptions should be propagated', async () => {
mockAxios.onPost('/parcels').networkError();

const error = await getRejection(client.deliverParcel(parcelSerialized, signer));
const error = await getPromiseRejection(client.deliverParcel(parcelSerialized, signer));

expect(error).toHaveProperty('isAxiosError', true);
});
Expand Down Expand Up @@ -1078,12 +1078,3 @@ describe('collectParcels', () => {
}
}
});

async function getRejection(promise: Promise<any>): Promise<Error> {
try {
await promise;
} catch (error) {
return error;
}
throw new Error('Expected promise to reject');
}
9 changes: 6 additions & 3 deletions src/lib/PoWebClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ export class PoWebClient implements GSCClient {
try {
return await PrivateNodeRegistration.deserialize(registrationSerialized);
} catch (exc) {
throw new ServerError(exc, 'Malformed registration received');
throw new ServerError(exc as Error, 'Malformed registration received');
}
}

Expand Down Expand Up @@ -270,7 +270,10 @@ export class PoWebClient implements GSCClient {
yield ParcelDelivery.deserialize(bufferToArray(parcelDeliverySerialized));
} catch (error) {
stateManager.registerServerProtocolViolation(
new ParcelDeliveryError(error, 'Received malformed parcel delivery from the server'),
new ParcelDeliveryError(
error as Error,
'Received malformed parcel delivery from the server',
),
{ code: WebSocketCode.CANNOT_ACCEPT, reason: 'Malformed parcel delivery' },
);
break;
Expand Down Expand Up @@ -339,7 +342,7 @@ export class PoWebClient implements GSCClient {
ws.close(WebSocketCode.CANNOT_ACCEPT, 'Malformed handshake challenge');
reject(
new InvalidHandshakeChallengeError(
error,
error as Error,
'Server sent a malformed handshake challenge',
),
);
Expand Down
6 changes: 2 additions & 4 deletions src/lib/_test_utils.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
export async function getPromiseRejection<ErrorType extends Error>(
promise: Promise<any>,
): Promise<ErrorType> {
export async function getPromiseRejection(promise: Promise<any>): Promise<Error> {
try {
await promise;
} catch (error) {
return error;
return error as Error;
}
throw new Error('Expected promise to throw');
}
Expand Down

0 comments on commit a4cac71

Please sign in to comment.