Skip to content
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: 3 additions & 3 deletions .talismanrc
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ fileignoreconfig:
- filename: src/functions/function.errors.test.ts
checksum: ac0a55e5bee974fefa9fb95541adba63e9a2c2c6e6c47b87c8ca1022542e75f0
- filename: src/transport/errors.test.ts
checksum: f38547e2a4639ae150b53f955963ce00c1922a4223c89e489ba5b7ca75a691d0
checksum: 72bd1e9af33dbb9dd4e86803534ebadbda37af5f0aeeefb4f99734cf027c7f39
- filename: src/transport/auth-strategy.ts
checksum: 6fe87def1b508cb67203d4445d9bd889570e512ed4194dfc4fdd3182bc8b87b2
- filename: src/functions/function.logger.test.ts
Expand Down Expand Up @@ -447,13 +447,13 @@ fileignoreconfig:
- filename: test/integration/deployments-rollback.test.ts
checksum: 08400c5435c84cd162fefc585f6c32cddfcaa8909d2369341748b421e7f12073
- filename: test/integration/deployments-cancel.test.ts
checksum: 02c012da974f1f9b8b851d7e00878fef50c1ba7e32b6acf72a309582c66aa5c2
checksum: 8972ed5f23e7dee8acf677ae9a26d2d28ef8144d03bc7623c5fd29f096a3aed0
- filename: src/commands/launch/deployments/cancel.test.ts
checksum: da8d09de7f03cfe67df75ebc1636b36feb6688cf85984c357fd5304852d126e5
- filename: src/commands/launch/deployments/rollback.test.ts
checksum: 5331e774d07387b1691c47168afa8d454bdee60d26ac9c6ed3f4640fc85ff8b8
- filename: src/deployments/deployments.api.test.ts
checksum: b27d91317c17f1754d5853c68f4968e63dc2b3579eab26b4c772cf3cd3efbdd0
checksum: 7a9b375ee156a6c61de30570814ce8f8109c90ca3863e760167ba018dd60b596
- filename: src/deployments/deployment.prompt.test.ts
checksum: 3367e53ce2f0284c14524da893e7f82ef613fcdac1e9689ac408709e7f7c153d
- filename: test/integration/deployments-request-bodies.test.ts
Expand Down
24 changes: 23 additions & 1 deletion src/deployments/deployments.api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
CANCEL_NOT_ALLOWED_RAW_MESSAGE,
DEPLOYMENT_ERROR_MESSAGES,
DeploymentUnsuccessfulError,
INVALID_DEPLOYMENT_UID_CODE,
INVALID_DEPLOYMENT_UID_RAW_MESSAGE,
} from './deployment.errors';
import { DeploymentsApi } from './deployments.api';
Expand Down Expand Up @@ -340,7 +341,7 @@ describe('DeploymentsApi', () => {
['the raw message as the code', [{ code: CANCEL_NOT_ALLOWED_RAW_MESSAGE, message: CANCEL_NOT_ALLOWED_RAW_MESSAGE }]],
['the raw message alone', [{ message: CANCEL_NOT_ALLOWED_RAW_MESSAGE }]],
['an empty error entry', [{}]],
['another message', [{ message: 'something else' }]],
['empty error entries only', [{}, {}]],
])('words a cancel refused with %s as the cancel-not-allowed message', async (_name, errors) => {
const rejection = new LaunchApiError(400, errors, DEPLOYMENT_ERROR_MESSAGES);
const client = { request: async () => Promise.reject(rejection) } as unknown as RestApiClient;
Expand Down Expand Up @@ -369,6 +370,27 @@ describe('DeploymentsApi', () => {
expect(failure.message).toBe(`Deployment "${DEPLOYMENT}" cannot be cancelled because it is no longer in progress.`);
});

it.each([
['another message', [{ message: 'something else' }]],
['no error entries, as a proxy answers', []],
['an empty entry beside another message', [{}, { message: 'something else' }]],
['a field-only entry', [{ field: 'deployment_uid' }]],
])('leaves a 400 cancel refusal carrying %s untouched', async (_name, errors) => {
const rejection = new LaunchApiError(400, errors, DEPLOYMENT_ERROR_MESSAGES);
const client = { request: async () => Promise.reject(rejection) } as unknown as RestApiClient;

await expect(new DeploymentsApi(client).cancel({ ...SCOPE, deployment: DEPLOYMENT })).rejects.toBe(rejection);
});

it('keeps the wording of another known code on a 400 cancel refusal', async () => {
const rejection = new LaunchApiError(400, [{ code: INVALID_DEPLOYMENT_UID_CODE }], DEPLOYMENT_ERROR_MESSAGES);
const client = { request: async () => Promise.reject(rejection) } as unknown as RestApiClient;

await expect(new DeploymentsApi(client).cancel({ ...SCOPE, deployment: DEPLOYMENT })).rejects.toThrow(
'The deployment UID is invalid. Provide a 24-character hexadecimal deployment UID.',
);
});

it('leaves a non-400 refusal untouched on cancel', async () => {
const wrongStatus = new LaunchApiError(409, [{ message: CANCEL_NOT_ALLOWED_RAW_MESSAGE }]);
const notFound = new LaunchApiError(404, [{ code: 'launch.DEPLOYMENT.NOT_FOUND', message: 'x' }], DEPLOYMENT_ERROR_MESSAGES);
Expand Down
20 changes: 18 additions & 2 deletions src/deployments/deployments.api.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { assertPage, hasUid, malformed, unwrap } from '../transport/envelope';
import type { RestApiClient, RestRequest } from '../transport/rest-client';
import { isRecord } from '../core/values';
import { LaunchApiError } from '../transport/errors';
import { LaunchApiError, type ApiErrorEntry } from '../transport/errors';
import {
CANCEL_NOT_ALLOWED_CODE,
CANCEL_NOT_ALLOWED_RAW_MESSAGE,
DEPLOYMENT_ERROR_MESSAGES,
DEPLOYMENT_NOT_FOUND_CODE,
cancelNotAllowedError,
Expand Down Expand Up @@ -38,10 +39,25 @@ export interface RollbackDeploymentParams extends DeploymentScope {
reason?: string;
}

function isRawRefusal(entry: ApiErrorEntry): boolean {
return entry.code === CANCEL_NOT_ALLOWED_RAW_MESSAGE || entry.message === CANCEL_NOT_ALLOWED_RAW_MESSAGE;
}

function isEmptyEntry(sent: unknown): boolean {
return isRecord(sent) && Object.keys(sent).length === 0;
}

function isCancelRaceRefusal(error: LaunchApiError): boolean {
return (
error.status === 400 &&
(error.errors.some(isRawRefusal) || (error.rawErrors.length > 0 && error.rawErrors.every(isEmptyEntry)))
);
}

function isCancelNotAllowed(error: unknown): error is LaunchApiError {
return (
error instanceof LaunchApiError &&
(error.status === 400 || error.errors.some((entry) => entry.code === CANCEL_NOT_ALLOWED_CODE))
(error.errors.some((entry) => entry.code === CANCEL_NOT_ALLOWED_CODE) || isCancelRaceRefusal(error))
);
}

Expand Down
14 changes: 14 additions & 0 deletions src/transport/errors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,20 @@ describe('parseErrorEnvelope', () => {
},
);

it('keeps the errors array exactly as the server sent it, beside the readable entries', () => {
const sent = [{}, 'deployment_uid is required', { error_message: 'x' }];

const error = parseErrorEnvelope(400, { errors: sent }, MESSAGES);

expect(error.errors).toEqual([{}, {}, {}]);
expect(error.rawErrors).toEqual(sent);
expect(parseErrorEnvelope(400, { status: 400 }, MESSAGES).rawErrors).toEqual([]);
});

it('treats the entries of a hand-built error as what was sent', () => {
expect(new LaunchApiError(400, [{ code: 'x' }]).rawErrors).toEqual([{ code: 'x' }]);
});

it('names the error LaunchApiError so it is recognisable once serialised', () => {
const error = parseErrorEnvelope(404, { errors: [{ code: 'launch.RESOURCE.NOT_FOUND', message: 'x' }] }, MESSAGES);

Expand Down
15 changes: 12 additions & 3 deletions src/transport/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,14 @@ export class LaunchApiError extends LaunchError {
readonly status: number;
readonly code: string;
readonly errors: ApiErrorEntry[];

constructor(status: number, errors: ApiErrorEntry[], messages: ErrorMessages = {}) {
readonly rawErrors: readonly unknown[];

constructor(
status: number,
errors: ApiErrorEntry[],
messages: ErrorMessages = {},
rawErrors: readonly unknown[] = errors,
) {
const [primary] = errors;
super(
messageForCode(messages, primary?.code) ??
Expand All @@ -57,6 +63,7 @@ export class LaunchApiError extends LaunchError {
this.status = status;
this.code = primary?.code ?? 'launch.UNKNOWN';
this.errors = errors;
this.rawErrors = rawErrors;
}
}

Expand Down Expand Up @@ -100,7 +107,9 @@ function entryFrom(value: unknown): ApiErrorEntry {
export function parseErrorEnvelope(status: number, body: unknown, messages: ErrorMessages = {}): LaunchApiError {
const errors = typeof body === 'object' && body !== null && 'errors' in body ? body.errors : undefined;

return new LaunchApiError(status, Array.isArray(errors) ? errors.map(entryFrom) : [], messages);
const sent: unknown[] = Array.isArray(errors) ? errors : [];

return new LaunchApiError(status, sent.map(entryFrom), messages, sent);
}

export const PROXY_ERROR_CODES: readonly string[] = ['ECONNREFUSED', 'ETIMEDOUT', 'ENOTFOUND', 'ERR_BAD_RESPONSE'];
Expand Down
22 changes: 22 additions & 0 deletions test/integration/deployments-cancel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const POST_PENDING = `POST ${LAUNCH_HUB_URL}:443${CANCEL_PATH}`;
const BODYLESS = { badheaders: ['content-type'] };
const RAW_REFUSAL = 'This deployment has progressed beyond the point where it can be cancelled.';
const CANCEL_WORDING = `Deployment "${DEPLOYMENT_UID}" cannot be cancelled because it is no longer in progress.`;
const STATUS_400_WORDING = 'Launch API request failed with status 400.';

let config: Interfaces.Config;
let onWire: string[];
Expand Down Expand Up @@ -264,6 +265,7 @@ describe('integration: launch:deployments:cancel on the wire', () => {
['the CANCEL_NOT_ALLOWED code', { code: 'launch.DEPLOYMENT.CANCEL_NOT_ALLOWED', message: 'x' }],
['the raw server message as the code', { code: RAW_REFUSAL, message: RAW_REFUSAL }],
['the raw server message alone', { message: RAW_REFUSAL }],
['an empty error entry', {}],
Comment thread
Harshi-Shah-CS marked this conversation as resolved.
Resolved
])('exits 1 with readable wording, posting once, when the cancel is refused with %s', async (_name, entry) => {
lookup();
const cancel = cancelScope()
Expand All @@ -279,6 +281,26 @@ describe('integration: launch:deployments:cancel on the wire', () => {
expect(onWire).toEqual([]);
});

it.each([
['no errors array', { status: 400 }, STATUS_400_WORDING],
['another message', { errors: [{ message: 'something else' }] }, 'something else'],
['a string entry', { errors: ['deployment_uid is required'] }, STATUS_400_WORDING],
['a null entry', { errors: [null] }, STATUS_400_WORDING],
['an array entry', { errors: [[]] }, STATUS_400_WORDING],
['a numeric code', { errors: [{ code: 400 }] }, STATUS_400_WORDING],
['an entry under other keys', { errors: [{ error_message: 'x' }] }, STATUS_400_WORDING],
['a field with a string value', { errors: [{ deployment_uid: 'must be a string' }] }, STATUS_400_WORDING],
])('keeps its own wording when the cancel is refused with a 400 carrying %s', async (_name, body, wording) => {
lookup();
const cancel = cancelScope().post(CANCEL_PATH).once().reply(400, body);

const { error } = await runCommand(args('--deployment', DEPLOYMENT_UID, '--yes'), config);

expect(error?.oclif?.exit).toBe(1);
expect(error?.message).toBe(wording);
expect(cancel.isDone()).toBe(true);
});

it.each([
['a finished deployment', getFixture],
['a deployment the service marks as not cancellable', { deployment: { ...IN_FLIGHT_FIXTURE.deployment, isCancellable: false } }],
Expand Down
Loading