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
5 changes: 4 additions & 1 deletion src/action-error.ts
Original file line number Diff line number Diff line change
@@ -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<string, any> | undefined {
return value !== null && typeof value === 'object' && !Array.isArray(value)
Expand Down Expand Up @@ -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,
Expand All @@ -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);
}
6 changes: 3 additions & 3 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down
8 changes: 8 additions & 0 deletions src/tests/action-error.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' });
});
Expand Down
Loading