Skip to content

Commit

Permalink
(fix, ts): Throw an error upon OAuth refresh failure (#3737)
Browse files Browse the repository at this point in the history
  • Loading branch information
amckinney authored May 31, 2024
1 parent 3c2977a commit 1ac00a0
Show file tree
Hide file tree
Showing 15 changed files with 138 additions and 73 deletions.
8 changes: 7 additions & 1 deletion generators/typescript/sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.20.5] - 2024-05-29
## [0.20.6] - 2024-05-31

- Fix: This updates the behavior of the failure condition introduced in `0.20.2`; the SDK
now throws an error whenever we fail to refresh an access token even if `neverThrowErrors`
is set. We treat this failure as a systematic exception, so it's OK to throw in this case.

## [0.20.5] - 2024-05-30

- Improvement: Support setting `extraPeerDependencies` and `extraPeerDependenciesMeta` as
configuration arguments. For example:
Expand Down
2 changes: 1 addition & 1 deletion generators/typescript/sdk/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.20.5
0.20.6
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ export class OAuthTokenProviderGenerator {
})}
${this.getRefreshMethod({
context,
getTokenEndpoint: endpoint,
requestProperties,
responseProperties
Expand All @@ -167,6 +168,10 @@ export class OAuthTokenProviderGenerator {
`;
}

private getGenericSdkErrorType({ context }: { context: SdkContext }): string {
return getTextOfTsNode(context.genericAPISdkError.getReferenceToGenericAPISdkError().getEntityName());
}

private getProperties({
authClientType,
clientIdType,
Expand Down Expand Up @@ -235,10 +240,12 @@ export class OAuthTokenProviderGenerator {
}

private getRefreshMethod({
context,
getTokenEndpoint,
requestProperties,
responseProperties
}: {
context: SdkContext;
getTokenEndpoint: HttpEndpoint;
requestProperties: OAuthAccessTokenRequestProperties;
responseProperties: OAuthAccessTokenResponseProperties;
Expand All @@ -248,7 +255,7 @@ export class OAuthTokenProviderGenerator {
const accessTokenProperty = this.responsePropertyToDotDelimitedAccessor({
responseProperty: responseProperties.accessToken
});
const handleNeverThrowErrors = this.getNeverThrowErrorsHandler();
const handleNeverThrowErrors = this.getNeverThrowErrorsHandler({ context });
if (responseProperties.expiresIn != null) {
const expiresInProperty = this.responsePropertyToDotDelimitedAccessor({
responseProperty: responseProperties.expiresIn
Expand Down Expand Up @@ -279,12 +286,37 @@ export class OAuthTokenProviderGenerator {
`;
}

private getNeverThrowErrorsHandler(): Code {
return this.neverThrowErrors
? code`if (!tokenResponse.ok) {
return this._accessToken ?? "";
}`
: code``;
private getNeverThrowErrorsHandler({ context }: { context: SdkContext }): Code {
if (!this.neverThrowErrors) {
return code``;
}
const errorType = this.getGenericSdkErrorType({ context });
return code`if (!tokenResponse.ok) {
switch (tokenResponse.error.content.reason) {
case "non-json":
throw new ${errorType}({
statusCode: tokenResponse.error.content.statusCode,
message: tokenResponse.error.content.rawBody
});
case "status-code":
throw new ${errorType}({
statusCode: tokenResponse.error.content.statusCode,
body: tokenResponse.error.content.body
});
case "timeout":
throw new ${errorType}({
message: "Failed to retrieve access token; request timed out"
});
case "unknown":
throw new ${errorType}({
message: tokenResponse.error.content.errorMessage
});
default:
throw new ${errorType}({
message: "Failed to retrieve access token"
});
}
}`;
}

private getExpiresAtMethod({
Expand Down
5 changes: 2 additions & 3 deletions generators/typescript/sdk/generator/src/SdkGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,6 @@ export class SdkGenerator {
private oauthTokenProviderGenerator: OAuthTokenProviderGenerator;
private jestTestGenerator: JestTestGenerator;
private FdrClient: FdrSnippetTemplateClient | undefined;
private generatedOAuthClients = false;

constructor({
namespaceExport,
Expand Down Expand Up @@ -363,7 +362,8 @@ export class SdkGenerator {

if (this.config.neverThrowErrors) {
this.generateEndpointErrorUnion();
} else {
}
if (!this.config.neverThrowErrors || this.generateOAuthClients) {
this.generateGenericAPISdkError();
this.generateTimeoutSdkError();
if (this.config.includeSerdeLayer) {
Expand All @@ -382,7 +382,6 @@ export class SdkGenerator {
const oauthScheme = this.intermediateRepresentation.auth.schemes.find((scheme) => scheme.type === "oauth");
if (oauthScheme != null && oauthScheme.type === "oauth") {
this.generateOAuthTokenProvider(oauthScheme);
this.generatedOAuthClients = true;
}
}

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

This file was deleted.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

This file was deleted.

0 comments on commit 1ac00a0

Please sign in to comment.