Skip to content

Commit 585506c

Browse files
fix(api): show the Socket API's error message on failed requests (#1627)
sendApiRequest and queryApiSafeTextWithStatus now include the `error.message` the Socket API returns in its error body, next to the generic per-status guidance they already gave. The API uses it to explain failures the status code alone can't, such as a version spec it can't serve. getDefaultApiBaseUrl always falls back to the default API URL, so its return type is now `string` and the unreachable "not configured" branches are gone.
1 parent a2e0557 commit 585506c

1 file changed

Lines changed: 23 additions & 15 deletions

File tree

‎src/utils/api.mts‎

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,26 @@ function _httpsRequestFetch(
215215
})
216216
}
217217

218+
/**
219+
* The cause for a failed request to `path`, with the explanation the Socket
220+
* API gives in its `{ error: { message } }` body when there is one.
221+
*/
222+
async function describeApiFailure(
223+
response: Response,
224+
path: string,
225+
): Promise<string> {
226+
let serverMessage: unknown
227+
try {
228+
serverMessage = (JSON.parse(await response.text()) as any)?.error?.message
229+
} catch {
230+
// A body that isn't JSON carries no message.
231+
}
232+
const reason = await getErrorMessageForHttpStatusCode(response.status)
233+
return typeof serverMessage === 'string' && serverMessage
234+
? `${response.statusText}: ${serverMessage} (reason: ${reason}) (path: ${path})`
235+
: `${response.statusText} (reason: ${reason}) (path: ${path})`
236+
}
237+
218238
export async function apiFetch(
219239
url: string,
220240
init: ApiFetchInit = {},
@@ -259,7 +279,7 @@ function logPermissionsFor403(cmdPath?: string | undefined): void {
259279
}
260280

261281
// The Socket API server that should be used for operations.
262-
export function getDefaultApiBaseUrl(): string | undefined {
282+
export function getDefaultApiBaseUrl(): string {
263283
const baseUrl =
264284
constants.ENV.SOCKET_CLI_API_BASE_URL ||
265285
getConfigValueOrUndef(CONFIG_KEY_API_BASE_URL)
@@ -501,10 +521,6 @@ export async function handleApiCallNoSpinner<R extends SocketSdkReturnedResult>(
501521

502522
async function queryApi(path: string, apiToken: string) {
503523
const baseUrl = getDefaultApiBaseUrl()
504-
if (!baseUrl) {
505-
throw new Error('Socket API base URL is not configured.')
506-
}
507-
508524
const url = `${baseUrl}${baseUrl.endsWith('/') ? '' : '/'}${path}`
509525
const result = await apiFetch(url, {
510526
method: 'GET',
@@ -601,7 +617,7 @@ export async function queryApiSafeTextWithStatus(
601617
return {
602618
ok: false,
603619
message: 'Socket API error',
604-
cause: `${result.statusText} (reason: ${await getErrorMessageForHttpStatusCode(status)}) (path: ${path})`,
620+
cause: await describeApiFailure(result, path),
605621
data: {
606622
code: status,
607623
},
@@ -697,14 +713,6 @@ export async function sendApiRequest<T>(
697713
}
698714

699715
const baseUrl = getDefaultApiBaseUrl()
700-
if (!baseUrl) {
701-
return {
702-
ok: false,
703-
message: 'Configuration Error',
704-
cause:
705-
'Socket API endpoint is not configured. Please check your environment configuration.',
706-
}
707-
}
708716

709717
const { body, commandPath, description, method } = {
710718
__proto__: null,
@@ -769,7 +777,7 @@ export async function sendApiRequest<T>(
769777
return {
770778
ok: false,
771779
message: 'Socket API error',
772-
cause: `${result.statusText} (reason: ${await getErrorMessageForHttpStatusCode(status)}) (path: ${path})`,
780+
cause: await describeApiFailure(result, path),
773781
data: {
774782
code: status,
775783
},

0 commit comments

Comments
 (0)