feat(common): display status text from the API response if available (#3466)

Co-authored-by: jamesgeorge007 <jamesgeorge998001@gmail.com>
This commit is contained in:
Gaurav K P
2023-12-04 23:31:49 +05:30
committed by GitHub
parent 8d5a456dbd
commit ed2a461dc5
5 changed files with 25 additions and 4 deletions

View File

@@ -29,10 +29,10 @@ function processResponse(
const contentLength = res.headers["content-length"]
? parseInt(res.headers["content-length"])
: (res.data as ArrayBuffer).byteLength
return <HoppRESTResponse>{
type: successState,
statusCode: res.status,
statusText: res.statusText,
body: res.data,
headers: Object.keys(res.headers).map((x) => ({
key: x,

View File

@@ -10,7 +10,7 @@ export type HoppRESTResponse =
headers: HoppRESTResponseHeader[]
body: ArrayBuffer
statusCode: number
statusText: string
meta: {
responseSize: number // in bytes
responseDuration: number // in millis
@@ -33,6 +33,7 @@ export type HoppRESTResponse =
headers: HoppRESTResponseHeader[]
body: ArrayBuffer
statusCode: number
statusText: string
meta: {
responseSize: number // in bytes
responseDuration: number // in millis

View File

@@ -85,6 +85,17 @@ const statusCodes: {
599: "Network connect timeout error", // (Unknown) This status code is not specified in any RFCs, but is used by Microsoft Corp. HTTP proxies to signal a network connect timeout behind the proxy to a client in front of the proxy.
}
export function getStatusCodeReasonPhrase(code: number): string {
export function getStatusCodeReasonPhrase(
code: number,
statusText?: string
): string {
// Return statusText if non-empty after trimming and add ellipsis if greater than 35 characters
const trimmedStatusText = statusText?.trim()
if (trimmedStatusText) {
return trimmedStatusText.length > 35
? `${trimmedStatusText.substring(0, 35)}...`
: trimmedStatusText
}
return statusCodes[code] ?? "Unknown"
}