Skip to content

Commit 25a2129

Browse files
authored
Merge pull request #49 from ainoya/improve-error-response
fix: improve error handling for AI service responses
2 parents bc9da18 + 59d0fd2 commit 25a2129

File tree

3 files changed

+36
-11
lines changed

3 files changed

+36
-11
lines changed

dist/index.js

Lines changed: 15 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,29 @@ export async function run(): Promise<void> {
8282
})
8383

8484
if (isUnexpected(response)) {
85-
if (response.body.error) {
85+
// Extract x-ms-error-code from headers if available
86+
const errorCode = response.headers['x-ms-error-code']
87+
const errorCodeMsg = errorCode ? ` (error code: ${errorCode})` : ''
88+
89+
// Check if response body exists and contains error details
90+
if (response.body && response.body.error) {
8691
throw response.body.error
8792
}
93+
94+
// Handle case where response body is missing
95+
if (!response.body) {
96+
throw new Error(
97+
`Failed to get response from AI service (status: ${response.status})${errorCodeMsg}. ` +
98+
'Please check network connection and endpoint configuration.'
99+
)
100+
}
101+
102+
// Handle other error cases
88103
throw new Error(
89-
'An error occurred while fetching the response (' +
90-
response.status +
91-
'): ' +
92-
response.body
104+
`AI service returned error response (status: ${response.status})${errorCodeMsg}: ` +
105+
(typeof response.body === 'string'
106+
? response.body
107+
: JSON.stringify(response.body))
93108
)
94109
}
95110

0 commit comments

Comments
 (0)