Skip to content
Open
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
10 changes: 8 additions & 2 deletions packages/core-internal/src/errors/sdkErrors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,15 @@ export class SdkError extends Error {
constructor(
public readonly code: SdkErrorCode,
message: string,
public readonly data?: unknown
public readonly data?: unknown,
options?: ErrorOptions
) {
super(message);
const errorOptions =
options ??
(data && typeof data === 'object' && 'cause' in data && (data as { cause: unknown }).cause !== undefined
? { cause: (data as { cause: unknown }).cause }
: undefined);
super(message, errorOptions);
this.name = 'SdkError';
stampErrorBrands(this, new.target);
}
Expand Down
17 changes: 17 additions & 0 deletions packages/core-internal/test/types/errorSurfacePins.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,23 @@ describe('SdkError', () => {
expect(error.message).toBe('Request timed out');
});

test('preserves error cause in Error.cause when passed via data.cause or options', () => {
const underlyingError = new TypeError('fetch failed: getaddrinfo ENOTFOUND host.invalid');

// When cause is passed via data object (as in classifyNetworkError)
const errorFromData = new SdkError(SdkErrorCode.EraNegotiationFailed, 'Version negotiation probe failed', {
cause: underlyingError
});
expect(errorFromData.cause).toBe(underlyingError);
expect(errorFromData.data).toEqual({ cause: underlyingError });

// When cause is passed via standard ErrorOptions
const errorFromOptions = new SdkError(SdkErrorCode.EraNegotiationFailed, 'Version negotiation probe failed', undefined, {
cause: underlyingError
});
expect(errorFromOptions.cause).toBe(underlyingError);
});

test('SdkHttpError carries the HTTP status in data', () => {
const error = new SdkHttpError(SdkErrorCode.ClientHttpFailedToOpenStream, 'Failed to open SSE stream: Not Found', {
status: 404,
Expand Down
Loading