-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
123 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { Deprecation } from "deprecation"; | ||
import once from "once"; | ||
const logOnce = once((deprecation: any) => console.warn(deprecation)); | ||
|
||
import { RequestOptions, ResponseHeaders, HttpErrorOptions } from "./types"; | ||
|
||
/** | ||
* Error with extra properties to help with debugging | ||
*/ | ||
export class HttpError extends Error { | ||
name: "HttpError"; | ||
|
||
/** | ||
* http status code | ||
*/ | ||
status: number; | ||
|
||
/** | ||
* http status code | ||
* | ||
* @deprecated `error.code` is deprecated in favor of `error.status` | ||
*/ | ||
code!: number; | ||
|
||
/** | ||
* error response headers | ||
*/ | ||
headers: ResponseHeaders; | ||
|
||
/** | ||
* Request options that lead to the error. | ||
*/ | ||
request: RequestOptions; | ||
|
||
constructor(message: string, statusCode: number, options: HttpErrorOptions) { | ||
super(message); | ||
|
||
// Maintains proper stack trace (only available on V8) | ||
/* istanbul ignore next */ | ||
if (Error.captureStackTrace) { | ||
Error.captureStackTrace(this, this.constructor); | ||
} | ||
|
||
this.name = "HttpError"; | ||
this.status = statusCode; | ||
Object.defineProperty(this, "code", { | ||
get() { | ||
logOnce( | ||
new Deprecation( | ||
"[@octokit/request-error] `error.code` is deprecated, use `error.status`." | ||
) | ||
); | ||
return statusCode; | ||
} | ||
}); | ||
this.headers = options.headers; | ||
|
||
// redact request credentials without mutating original request options | ||
const requestCopy = Object.assign({}, options.request); | ||
if (options.request.headers.authorization) { | ||
requestCopy.headers = Object.assign({}, options.request.headers, { | ||
authorization: options.request.headers.authorization.replace( | ||
/ .*$/, | ||
" [REDACTED]" | ||
) | ||
}); | ||
} | ||
|
||
// client_id & client_secret can be passed as URL query parameters to increase rate limit | ||
// see https://developer.github.com/v3/#increasing-the-unauthenticated-rate-limit-for-oauth-applications | ||
requestCopy.url = requestCopy.url.replace( | ||
/\bclient_secret=\w+/g, | ||
"client_secret=[REDACTED]" | ||
); | ||
|
||
this.request = requestCopy; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/** | ||
* Relative or absolute URL. Examples: `'/orgs/:org'`, `https://example.com/foo/bar` | ||
*/ | ||
export type Url = string; | ||
|
||
/** | ||
* Request method | ||
*/ | ||
export type Method = "DELETE" | "GET" | "HEAD" | "PATCH" | "POST" | "PUT"; | ||
|
||
export type RequestHeaders = { | ||
/** | ||
* Used for API previews and custom formats | ||
*/ | ||
accept?: string; | ||
/** | ||
* Redacted authorization header | ||
*/ | ||
authorization?: string; | ||
|
||
"user-agent"?: string; | ||
|
||
[header: string]: string | number | undefined; | ||
}; | ||
|
||
export type ResponseHeaders = { | ||
[header: string]: string; | ||
}; | ||
|
||
export type EndpointRequestOptions = { | ||
[option: string]: any; | ||
}; | ||
|
||
export type RequestOptions = { | ||
method: Method; | ||
url: Url; | ||
headers: RequestHeaders; | ||
body?: any; | ||
request?: EndpointRequestOptions; | ||
}; | ||
|
||
export type HttpErrorOptions = { | ||
headers: ResponseHeaders; | ||
request: RequestOptions; | ||
}; |