diff --git a/.talismanrc b/.talismanrc index a2cb1be7..0f96875b 100644 --- a/.talismanrc +++ b/.talismanrc @@ -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 @@ -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 diff --git a/src/deployments/deployments.api.test.ts b/src/deployments/deployments.api.test.ts index a8d89fad..6af754b2 100644 --- a/src/deployments/deployments.api.test.ts +++ b/src/deployments/deployments.api.test.ts @@ -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'; @@ -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; @@ -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); diff --git a/src/deployments/deployments.api.ts b/src/deployments/deployments.api.ts index e172413c..6592d907 100644 --- a/src/deployments/deployments.api.ts +++ b/src/deployments/deployments.api.ts @@ -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, @@ -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)) ); } diff --git a/src/transport/errors.test.ts b/src/transport/errors.test.ts index c5d1daa4..5ab9d21a 100644 --- a/src/transport/errors.test.ts +++ b/src/transport/errors.test.ts @@ -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); diff --git a/src/transport/errors.ts b/src/transport/errors.ts index 982e83d0..3fe48834 100644 --- a/src/transport/errors.ts +++ b/src/transport/errors.ts @@ -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) ?? @@ -57,6 +63,7 @@ export class LaunchApiError extends LaunchError { this.status = status; this.code = primary?.code ?? 'launch.UNKNOWN'; this.errors = errors; + this.rawErrors = rawErrors; } } @@ -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']; diff --git a/test/integration/deployments-cancel.test.ts b/test/integration/deployments-cancel.test.ts index 7dc79a96..482c2ba3 100644 --- a/test/integration/deployments-cancel.test.ts +++ b/test/integration/deployments-cancel.test.ts @@ -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[]; @@ -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', {}], ])('exits 1 with readable wording, posting once, when the cancel is refused with %s', async (_name, entry) => { lookup(); const cancel = cancelScope() @@ -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 } }],