Skip to content
This repository was archived by the owner on Jan 3, 2026. It is now read-only.

feat!: Support AIP-193 for GaxiosError#699

Merged
d-goog merged 10 commits intomainfrom
aip-193-gaxios-error
May 23, 2025
Merged

feat!: Support AIP-193 for GaxiosError#699
d-goog merged 10 commits intomainfrom
aip-193-gaxios-error

Conversation

@d-goog
Copy link
Contributor

@d-goog d-goog commented May 23, 2025

Description

Adds AIP-193 recommendations to GaxiosError, including:

  • Surfacing API error messages as its message (Error.message)
  • Adding the content of the API error into Gaxios's cause property, which has first-class logging support in many environments (Error.cause)

History

Previously, some of this functionality was in google-auth-library within its now-removed transporter. It was within the transporter this logic predates this library itself (we were using request and axios at the time) and auth was the closest place to put such logic. This logic can now live where it belongs - gaxios.

Some additional references for background:

Impact

This will improve the debugging experience for customers as the API-level error is easy to see while the GaxiosError context/details are preserved.

Testing

Updated tests to reflect. Note: many tests did not require changing as it is mostly backwards-compatible.

Additional Information

Fixes #572
Fixes #684

🦕

@d-goog d-goog requested a review from a team as a code owner May 23, 2025 14:51
@product-auto-label product-auto-label bot added the size: m Pull request size is medium. label May 23, 2025
@generated-files-bot
Copy link

Warning: This pull request is touching the following templated files:

@d-goog d-goog changed the title feat!: Support AIP-193 for GaxiosError feat!: Support AIP-193 for GaxiosError May 23, 2025
err.response?.data.error.message === 'File not found'
);

assert.deepStrictEqual(err.cause, body.error);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note: this assert works within the callback context as assert.rejects handles it properly.

Comment on lines -106 to +107
(err.config.signal?.aborted && err.error?.name !== 'TimeoutError') ||
err.name === 'AbortError' ||
err.error?.name === 'AbortError'
(err.config.signal?.aborted && err.code !== 'TimeoutError') ||
err.code === 'AbortError'
Copy link
Contributor Author

Choose a reason for hiding this comment

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

A few things here:

  • name could never be AbortError (always GaxiosError)
  • We can demonstrate best practices by not referencing the deprecated GaxiosError#error
  • Notice the relevant retry tests did not need to change for this logic

Comment on lines +219 to +227
let err: GaxiosError;

if (e instanceof GaxiosError) {
err = e;
} else if (e instanceof Error) {
err = new GaxiosError(e.message, opts, undefined, e);
} else {
err = new GaxiosError('Unexpected Gaxios Error', opts, undefined, e);
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Summary:

  • in the first case we're using the existing GaxiosError
  • in the second we're adopting an existing Error's message
  • in the third some random, non-Error must've been thrown

Note:

  • GaxiosError supports e as unknown

Comment on lines +182 to +183
* @internal
* @expiremental
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We can open this up for external libraries later, if needed

Comment on lines +162 to +174
if (cause instanceof DOMException) {
// The DOMException's equivalent to code is its name
// E.g.: name = `TimeoutError`, code = number
// https://developer.mozilla.org/en-US/docs/Web/API/DOMException/name
this.code = cause.name;
} else if (
cause &&
typeof cause === 'object' &&
'code' in cause &&
typeof cause.code === 'string'
) {
this.code = cause.code;
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Improved accuracy and type-checking as cause is unknown. DOMException is the only standard Error instance that has a numeric code rather than a string (and that property is deprecated).

## License

[Apache-2.0](https://github.com/googleapis/gaxios/blob/master/LICENSE)
[Apache-2.0](https://github.com/googleapis/gaxios/blob/main/LICENSE)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

linkinator complaint

},
"devDependencies": {
"@octokit/rest": "^21.0.0",
"@octokit/rest": "^19.0.0",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

via owlbot

src/common.ts Outdated
cause &&
typeof cause === 'object' &&
'code' in cause &&
typeof cause.code === 'string'
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
typeof cause.code === 'string'

Copy link
Contributor

Choose a reason for hiding this comment

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

Currently code comes as int32 so this would cause code to be undefined in a lot of places.

@sofisl sofisl changed the title feat!: Support AIP-193 for GaxiosError fix!: Support AIP-193 for GaxiosError May 23, 2025
Copy link
Contributor

@sofisl sofisl left a comment

Choose a reason for hiding this comment

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

These changes mirror the previous processError function and pass current tests in google-api-nodejs-client.

@d-goog d-goog changed the title fix!: Support AIP-193 for GaxiosError feat!: Support AIP-193 for GaxiosError May 23, 2025
@sofisl
Copy link
Contributor

sofisl commented May 23, 2025

Commenting here so we can remember...

It looks like the decision between returning error as code or string was already a discussion, Ben brought up the point that it could/should be either so let's just pass it through (without type-checking).

@d-goog d-goog merged commit b6e0032 into main May 23, 2025
14 checks passed
@d-goog d-goog deleted the aip-193-gaxios-error branch May 23, 2025 19:50
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

size: m Pull request size is medium.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PRovide in-depth details on error messages feat!: Use Native cause in GaxiosError

2 participants