1
0
mirror of https://github.com/sasjs/adapter.git synced 2026-01-07 20:40:05 +00:00

fix(error-handling): added InternalServerError

This commit is contained in:
Yury Shkoda
2021-03-10 14:43:47 +03:00
parent 599c130395
commit 719135e366
16 changed files with 76 additions and 34 deletions

View File

@@ -1,9 +1,13 @@
import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios'
import { CsrfToken, JobExecutionError } from '..'
import { CsrfToken } from '..'
import { isAuthorizeFormRequired, isLogInRequired } from '../auth'
import { LoginRequiredError } from '../types'
import { AuthorizeError } from '../types/AuthorizeError'
import { NotFoundError } from '../types/NotFoundError'
import {
AuthorizeError,
LoginRequiredError,
NotFoundError,
InternalServerError,
JobExecutionError
} from '../types/errors'
import { parseWeboutResponse } from '../utils/parseWeboutResponse'
import { prefixMessage } from '@sasjs/utils/error'
@@ -73,7 +77,8 @@ export class RequestClient implements HttpClient {
url: string,
accessToken: string | undefined,
contentType: string = 'application/json',
overrideHeaders: { [key: string]: string | number } = {}
overrideHeaders: { [key: string]: string | number } = {},
debug: boolean = false
): Promise<{ result: T; etag: string }> {
const headers = {
...this.getHeaders(accessToken, contentType),
@@ -97,15 +102,18 @@ export class RequestClient implements HttpClient {
return this.parseResponse<T>(response)
})
.catch(async (e) => {
return await this.handleError(e, () =>
this.get<T>(url, accessToken, contentType, overrideHeaders).catch(
(err) => {
throw prefixMessage(
err,
'Error while executing handle error callback. '
)
}
)
return await this.handleError(
e,
() =>
this.get<T>(url, accessToken, contentType, overrideHeaders).catch(
(err) => {
throw prefixMessage(
err,
'Error while executing handle error callback. '
)
}
),
debug
).catch((err) => {
throw prefixMessage(err, 'Error while handling error. ')
})
@@ -340,7 +348,11 @@ export class RequestClient implements HttpClient {
}
}
private handleError = async (e: any, callback: any) => {
private handleError = async (
e: any,
callback: any,
debug: boolean = false
) => {
const response = e.response as AxiosResponse
if (e instanceof AuthorizeError) {
@@ -386,10 +398,11 @@ export class RequestClient implements HttpClient {
throw e
} else if (response?.status === 404) {
throw new NotFoundError(response.config.url!)
} else if (response?.status === 502) {
if (debug) throw new InternalServerError()
else return
}
console.log(`[e]`, e)
throw e
}