Skip to content

Commit

Permalink
fix(enterprise): handle requests that fail before reaching the server
Browse files Browse the repository at this point in the history
  • Loading branch information
eysi09 authored and thsig committed Mar 9, 2021
1 parent a34fa07 commit c21a1b4
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 51 deletions.
15 changes: 7 additions & 8 deletions core/src/enterprise/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

import { got, GotResponse, GotHeaders } from "../util/http"
import { got, GotResponse, GotHeaders, GotHttpError } from "../util/http"
import { findProjectConfig } from "../config/base"
import { CommandError, RuntimeError } from "../exceptions"
import { LogEntry } from "../logger/log-entry"
Expand All @@ -23,6 +23,10 @@ export const authTokenHeader =

export const makeAuthHeader = (clientAuthToken: string) => ({ [authTokenHeader]: clientAuthToken })

function is401Error(error: any): error is GotHttpError {
return error instanceof GotHttpError && error.response.statusCode === 401
}

const refreshThreshold = 10 // Threshold (in seconds) subtracted to jwt validity when checking if a refresh is needed

export interface ApiFetchParams {
Expand Down Expand Up @@ -153,9 +157,7 @@ export class EnterpriseApi {
}
await this.saveAuthToken(tokenObj)
} catch (err) {
const res = err.response

if (res && res.statusCode === 401) {
if (is401Error(err)) {
this.log.debug({ msg: `Failed to refresh the token.` })
await this.clearAuthToken()
throw new RuntimeError(invalidCredentialsErrorMsg, {})
Expand Down Expand Up @@ -289,10 +291,7 @@ export class EnterpriseApi {
await this.get(log, "token/verify")
valid = true
} catch (err) {
const res = err.response
if (res.statusCode === 401) {
valid = false
} else {
if (!is401Error(err)) {
throw new RuntimeError(`An error occurred while verifying client auth token with platform: ${err.message}`, {})
}
}
Expand Down
42 changes: 0 additions & 42 deletions core/src/enterprise/secrets/index.ts

This file was deleted.

3 changes: 2 additions & 1 deletion core/src/util/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

import _got, { Response } from "got"
import _got, { Response, HTTPError as GotHttpError } from "got"
import { bootstrap } from "global-agent"
import { OptionsOfTextResponseBody, Headers } from "got"

Expand All @@ -22,3 +22,4 @@ export const got = _got
export type GotOptions = OptionsOfTextResponseBody
export type GotResponse<T = unknown> = Response<T>
export type GotHeaders = Headers
export { GotHttpError }

0 comments on commit c21a1b4

Please sign in to comment.