diff --git a/src/action-error.ts b/src/action-error.ts index b539f19..50b2a01 100644 --- a/src/action-error.ts +++ b/src/action-error.ts @@ -1,4 +1,4 @@ -import { BusinessResponseError, HttpError, RequestTimeoutError } from './client.ts'; +import { BusinessResponseError, HttpError, PREVIEW_UNSUPPORTED_HINT, RequestTimeoutError } from './client.ts'; function record(value: unknown): Record | undefined { return value !== null && typeof value === 'object' && !Array.isArray(value) @@ -41,6 +41,8 @@ export function actionErrorDetail(error: unknown, apiKey?: string, envelope?: un ?? (error instanceof RequestTimeoutError ? 'REQUEST_TIMEOUT' : status ? `HTTP_${status}` : unknown ? 'EXECUTION_OUTCOME_UNKNOWN' : 'ACTION_FAILED'); const details = inner?.details ?? body?.details ?? (Array.isArray(message) ? message : undefined); + // call errors are rebuilt from the response body, so the client's message hint is lost here. + const previewUnsupported = status === 400 && JSON.stringify([message, details]).includes('include_preview'); return redactActionFailure({ ...(status ? { status } : {}), code, @@ -49,5 +51,6 @@ export function actionErrorDetail(error: unknown, apiKey?: string, envelope?: un ...(error instanceof HttpError && error.retryAfterMs !== undefined ? { retry_after_ms: error.retryAfterMs } : {}), outcome: unknown ? 'unknown' : 'failed', ...(unknown ? { hint: 'The upstream may have completed the action. Check its result before retrying a write.' } : {}), + ...(previewUnsupported ? { hint: PREVIEW_UNSUPPORTED_HINT } : {}), }, apiKey); } diff --git a/src/client.ts b/src/client.ts index 33e8c16..b5fae2f 100644 --- a/src/client.ts +++ b/src/client.ts @@ -124,10 +124,10 @@ export interface ClientOptions { } /** Backends that predate preview reject the body field (`property include_preview should not exist`). */ +export const PREVIEW_UNSUPPORTED_HINT = '--preview needs an xAPI backend with preview support; on older backends a key with the legacy sandbox permission reaches your own unpublished services without --preview'; + export function withPreviewHint(status: number, message: string): string { - return status === 400 && message.includes('include_preview') - ? `${message} (--preview needs an xAPI backend with preview support; on older backends a key with the legacy sandbox permission reaches your own unpublished services without --preview)` - : message; + return status === 400 && message.includes('include_preview') ? `${message} (${PREVIEW_UNSUPPORTED_HINT})` : message; } /** Older backends may reject unknown body fields, so send include_preview only when requested. */ diff --git a/src/tests/action-error.test.ts b/src/tests/action-error.test.ts index de12f3f..a5479e7 100644 --- a/src/tests/action-error.test.ts +++ b/src/tests/action-error.test.ts @@ -3,6 +3,14 @@ import { actionErrorDetail, redactActionFailure } from '../action-error.ts'; import { HttpError, RequestTimeoutError } from '../client.ts'; describe('action error details', () => { + test('call --preview against a backend without preview support explains the 400', () => { + const body = { statusCode: 400, message: ['property include_preview should not exist'], error: 'Bad Request' }; + expect(actionErrorDetail(new HttpError(400, JSON.stringify(body), undefined, body))).toMatchObject({ + status: 400, outcome: 'failed', hint: expect.stringContaining('--preview needs an xAPI backend with preview support'), + }); + const other = { statusCode: 400, message: ['name must be a string'] }; + expect(actionErrorDetail(new HttpError(400, JSON.stringify(other), undefined, other))).not.toHaveProperty('hint'); + }); test('timeout stays unknown and warns against replaying a write', () => { expect(actionErrorDetail(new RequestTimeoutError(60_000))).toMatchObject({ code: 'REQUEST_TIMEOUT', outcome: 'unknown' }); });