Skip to content

fix: improve error handling for AI service responses #49

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

ainoya
Copy link

@ainoya ainoya commented Jul 2, 2025

Improve error handling to prevent undefined property access errors and add x-ms-error-code header information for better debugging.

- Add defensive check for response.body existence to prevent undefined property access
- Include x-ms-error-code header in error messages for better debugging
- Provide clearer error messages for different failure scenarios
- Fix 'Cannot read properties of undefined (reading 'error')' runtime error

This improves the debugging experience when AI service requests fail due to
network issues, authentication errors, or unexpected response formats.
@Copilot Copilot AI review requested due to automatic review settings July 2, 2025 00:22
@ainoya ainoya requested a review from a team as a code owner July 2, 2025 00:22
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Improves robustness of AI service response handling by extracting header error codes and preventing undefined body access.

  • Extract x-ms-error-code from response headers for richer error context
  • Guard against missing response.body before property access
  • Enhance thrown error messages with status, error code, and serialized body
Comments suppressed due to low confidence (1)

src/main.ts:95

  • This branch handles missing response.body—add unit tests to verify that the correct error is thrown when the body is undefined.
      if (!response.body) {

@@ -82,14 +82,27 @@ export async function run(): Promise<void> {
})

if (isUnexpected(response)) {
if (response.body.error) {
// Extract x-ms-error-code from headers if available
const errorCode = response.headers['x-ms-error-code']
Copy link
Preview

Copilot AI Jul 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Depending on your HTTP client, headers may need to be accessed via a get method (e.g., response.headers.get('x-ms-error-code')) rather than direct property indexing.

Suggested change
const errorCode = response.headers['x-ms-error-code']
const errorCode = response.headers.get('x-ms-error-code')

Copilot uses AI. Check for mistakes.

const errorCodeMsg = errorCode ? ` (error code: ${errorCode})` : ''

// Check if response body exists and contains error details
if (response.body && response.body.error) {
throw response.body.error
Copy link
Preview

Copilot AI Jul 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Throwing response.body.error directly may not yield an Error instance or include the errorCodeMsg. Consider wrapping it in an Error, for example: throw new Error(${response.body.error.message}${errorCodeMsg}).

Suggested change
throw response.body.error
throw new Error(`${response.body.error.message}${errorCodeMsg}`)

Copilot uses AI. Check for mistakes.

sgoedecke
sgoedecke previously approved these changes Jul 2, 2025
Copy link
Collaborator

@sgoedecke sgoedecke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your interest in actions/ai-inference! I'm broadly happy with this approach (once the tests are green and linting has passed).

@ainoya
Copy link
Author

ainoya commented Jul 2, 2025

@sgoedecke Thanks for the review! I’ve fixed the linting issues and all unit tests are now passing. Unfortunately, the integration tests are still failing:

Error: Failed to get response from AI service (status: 403). Please check network connection and endpoint configuration.

The improved error message you see above comes from this PR, so the failure itself may be caused by something outside these changes. Could you take a look when you have a chance?
CI run for reference: https://github.com/actions/ai-inference/actions/runs/16013522828/job/45175915880?pr=49#step:3:12

@ainoya ainoya requested a review from sgoedecke July 2, 2025 01:06
@sgoedecke
Copy link
Collaborator

sgoedecke commented Jul 2, 2025

Yeah, you're right - the errors are because Models isn't enabled for the actions organization. Let me follow up on that.

@sgoedecke sgoedecke enabled auto-merge July 2, 2025 01:15
@sgoedecke sgoedecke disabled auto-merge July 2, 2025 01:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants