From 31dc2479be1576b94b81b29abfa4d13b7f3082d6 Mon Sep 17 00:00:00 2001 From: Eric Anderson Date: Fri, 12 Jul 2024 11:35:34 -0500 Subject: [PATCH] Add more logging to legacy-client oauth (#479) (#480) --- .changeset/odd-hornets-protect.md | 5 ++++ packages/legacy-client/src/client/addCause.ts | 27 +++++++++++++++++++ .../ConfidentialClientFlow.ts | 15 ++++++++--- .../PublicClient/PublicClientFlowProvider.ts | 22 ++++++++++----- 4 files changed, 59 insertions(+), 10 deletions(-) create mode 100644 .changeset/odd-hornets-protect.md create mode 100644 packages/legacy-client/src/client/addCause.ts diff --git a/.changeset/odd-hornets-protect.md b/.changeset/odd-hornets-protect.md new file mode 100644 index 000000000..cb9d120cc --- /dev/null +++ b/.changeset/odd-hornets-protect.md @@ -0,0 +1,5 @@ +--- +"@osdk/legacy-client": patch +--- + +Oauth errors should include a cause now on supported platforms diff --git a/packages/legacy-client/src/client/addCause.ts b/packages/legacy-client/src/client/addCause.ts new file mode 100644 index 000000000..1d589a787 --- /dev/null +++ b/packages/legacy-client/src/client/addCause.ts @@ -0,0 +1,27 @@ +/* + * Copyright 2024 Palantir Technologies, Inc. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export function addCause(e: Error, cause: any) { + if (process.env.NODE_ENV !== "production") { + // In case someone is debugging on an older browser or the `.cause` assignment trick + // does not work in certain environments, we log the cause to the console to aid debugging. + // eslint-disable-next-line no-console + console.error("Preparing to throw error due to", cause); + } + + (e as any).cause = cause; + return e; +} diff --git a/packages/legacy-client/src/oauth-client/ConfidentialClient/ConfidentialClientFlow.ts b/packages/legacy-client/src/oauth-client/ConfidentialClient/ConfidentialClientFlow.ts index 897cfebd9..ca09bb353 100644 --- a/packages/legacy-client/src/oauth-client/ConfidentialClient/ConfidentialClientFlow.ts +++ b/packages/legacy-client/src/oauth-client/ConfidentialClient/ConfidentialClientFlow.ts @@ -14,6 +14,7 @@ * limitations under the License. */ +import { addCause } from "../../client/addCause"; import { OAuthToken } from "../OAuthToken"; import { fetchFormEncoded, getRevokeUri, getTokenUri } from "../utils"; @@ -43,8 +44,11 @@ export async function getTokenWithClientSecret( const responseText = await response.json(); return new OAuthToken(responseText); } catch (e) { - throw new Error( - `Failed to get token: ${e ? e.toString() : "Unknown error"}`, + throw addCause( + new Error( + `Failed to get token: ${e ? e.toString() : "Unknown error"}`, + ), + e, ); } } @@ -66,8 +70,11 @@ export async function revokeTokenWithClientSecret( try { await fetchFormEncoded(fetchFn, tokenUrl, body); } catch (e) { - throw new Error( - `Failed to revoke token: ${e ? e.toString() : "Unknown error"}`, + throw addCause( + new Error( + `Failed to revoke token: ${e ? e.toString() : "Unknown error"}`, + ), + e, ); } } diff --git a/packages/legacy-client/src/oauth-client/PublicClient/PublicClientFlowProvider.ts b/packages/legacy-client/src/oauth-client/PublicClient/PublicClientFlowProvider.ts index 324a182b9..1cffa2754 100644 --- a/packages/legacy-client/src/oauth-client/PublicClient/PublicClientFlowProvider.ts +++ b/packages/legacy-client/src/oauth-client/PublicClient/PublicClientFlowProvider.ts @@ -14,6 +14,7 @@ * limitations under the License. */ +import { addCause } from "../../client/addCause"; import { OAuthToken } from "../OAuthToken"; import { fetchFormEncoded, @@ -101,8 +102,11 @@ export async function getTokenWithCodeVerifier( const responseText = await response.json(); return new OAuthToken(responseText); } catch (e) { - throw new Error( - `Failed to get token: ${e ? e.toString() : "Unknown error"}`, + throw addCause( + new Error( + `Failed to get token: ${e ? e.toString() : "Unknown error"}`, + ), + e, ); } } @@ -125,8 +129,11 @@ export async function refreshTokenPublicClient( const responseText = await response.json(); return new OAuthToken(responseText); } catch (e) { - throw new Error( - `Failed to refresh token: ${e ? e.toString() : "Unknown error"}`, + throw addCause( + new Error( + `Failed to refresh token: ${e ? e.toString() : "Unknown error"}`, + ), + e, ); } } @@ -146,8 +153,11 @@ export async function revokeTokenPublicClient( try { await fetchFormEncoded(fetchFn, tokenUrl, body); } catch (e) { - throw new Error( - `Failed to revoke token: ${e ? e.toString() : "Unknown error"}`, + throw addCause( + new Error( + `Failed to revoke token: ${e ? e.toString() : "Unknown error"}`, + ), + e, ); } }