Skip to content

Commit

Permalink
Add more logging to legacy-client oauth (#479) (#480)
Browse files Browse the repository at this point in the history
  • Loading branch information
ericanderson authored Jul 12, 2024
1 parent 928dd8b commit 31dc247
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/odd-hornets-protect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@osdk/legacy-client": patch
---

Oauth errors should include a cause now on supported platforms
27 changes: 27 additions & 0 deletions packages/legacy-client/src/client/addCause.ts
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

import { addCause } from "../../client/addCause";
import { OAuthToken } from "../OAuthToken";
import { fetchFormEncoded, getRevokeUri, getTokenUri } from "../utils";

Expand Down Expand Up @@ -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,
);
}
}
Expand All @@ -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,
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

import { addCause } from "../../client/addCause";
import { OAuthToken } from "../OAuthToken";
import {
fetchFormEncoded,
Expand Down Expand Up @@ -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,
);
}
}
Expand All @@ -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,
);
}
}
Expand All @@ -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,
);
}
}

0 comments on commit 31dc247

Please sign in to comment.