Skip to content
Closed
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
Empty file.
18 changes: 18 additions & 0 deletions packages/http-client/src/http-client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
import * as http from 'http';
import * as https from 'https';

//
import { HttpRequestError } from './http-request-error';

// FIND the catch block (looks roughly like this) and change it:
// BEFORE:
} catch (e) {
throw e;
}

// AFTER:
} catch (e) {
if (isAxiosError(e) && e.response) {
throw new HttpRequestError(e);
}
throw e;
}

import {
buildHeadersForDestination,
getAgentConfig,
Expand Down
Empty file.
37 changes: 37 additions & 0 deletions packages/http-client/src/http-request-error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { AxiosError } from 'axios';

/**
* Typed error thrown by executeHttpRequest when the server
* returns a non-2xx response. Surfaces the response body so
* callers can inspect SAP OData/REST error payloads.
*/
export class HttpRequestError extends Error {
readonly statusCode: number | undefined;
readonly responseBody: unknown;
readonly cause: AxiosError;

constructor(axiosError: AxiosError) {
const status = axiosError.response?.status;
const body = axiosError.response?.data;
const sapMessage = extractSapErrorMessage(body);
const baseMessage = axiosError.message;

super(
sapMessage
? `Request failed with status ${status}: ${sapMessage}`
: `Request failed with status ${status}: ${baseMessage}`
);

this.name = 'HttpRequestError';
this.statusCode = status;
this.responseBody = body;
this.cause = axiosError;
}
}

function extractSapErrorMessage(body: unknown): string | undefined {
if (!body || typeof body !== 'object') return undefined;
const err = (body as any)?.error;
const msg = err?.message?.value ?? err?.message ?? err?.Message;
return typeof msg === 'string' ? msg : undefined;
}
1 change: 1 addition & 0 deletions packages/http-client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@ export type {
ParameterEncoder
} from './http-client-types';
export { defaultDisallowedKeys } from './http-request-config';
export { HttpRequestError } from './http-request-error';