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

Merge branch 'master' into issue-258

This commit is contained in:
Krishna Acondy
2021-02-25 07:59:43 +00:00
committed by GitHub
2 changed files with 35 additions and 9 deletions

4
.gitignore vendored
View File

@@ -3,4 +3,6 @@ build
.env .env
/coverage /coverage
.DS_Store

View File

@@ -5,6 +5,7 @@ import { LoginRequiredError } from '../types'
import { AuthorizeError } from '../types/AuthorizeError' import { AuthorizeError } from '../types/AuthorizeError'
import { NotFoundError } from '../types/NotFoundError' import { NotFoundError } from '../types/NotFoundError'
import { parseWeboutResponse } from '../utils/parseWeboutResponse' import { parseWeboutResponse } from '../utils/parseWeboutResponse'
import { prefixMessage } from '@sasjs/utils/error'
export interface HttpClient { export interface HttpClient {
get<T>( get<T>(
@@ -97,7 +98,9 @@ export class RequestClient implements HttpClient {
.catch(async (e) => { .catch(async (e) => {
return await this.handleError(e, () => return await this.handleError(e, () =>
this.get<T>(url, accessToken, contentType, overrideHeaders) this.get<T>(url, accessToken, contentType, overrideHeaders)
) ).catch((err) => {
throw prefixMessage(err, 'Error while handling error. ')
})
}) })
} }
@@ -331,33 +334,54 @@ export class RequestClient implements HttpClient {
private handleError = async (e: any, callback: any) => { private handleError = async (e: any, callback: any) => {
const response = e.response as AxiosResponse const response = e.response as AxiosResponse
if (e instanceof AuthorizeError) { if (e instanceof AuthorizeError) {
const res = await this.httpClient.get(e.confirmUrl, { const res = await this.httpClient
responseType: 'text', .get(e.confirmUrl, {
headers: { 'Content-Type': 'text/plain', Accept: '*/*' } responseType: 'text',
}) headers: { 'Content-Type': 'text/plain', Accept: '*/*' }
})
.catch((err) => {
throw prefixMessage(err, 'Error while getting error confirmUrl. ')
})
if (isAuthorizeFormRequired(res?.data as string)) { if (isAuthorizeFormRequired(res?.data as string)) {
await this.authorize(res.data as string) await this.authorize(res.data as string)
} }
return await callback()
return await callback().catch((err: any) => {
throw prefixMessage(
err,
'Error while executing callback in handleError. '
)
})
} }
if (e instanceof LoginRequiredError) { if (e instanceof LoginRequiredError) {
this.clearCsrfTokens() this.clearCsrfTokens()
} }
if (response?.status === 403 || response?.status === 449) { if (response?.status === 403 || response?.status === 449) {
this.parseAndSetCsrfToken(response) this.parseAndSetCsrfToken(response)
if (this.csrfToken.headerName && this.csrfToken.value) { if (this.csrfToken.headerName && this.csrfToken.value) {
return await callback() return await callback().catch((err: any) => {
throw prefixMessage(
err,
'Error while executing callback in handleError. '
)
})
} }
throw e throw e
} else if (response?.status === 404) { } else if (response?.status === 404) {
throw new NotFoundError(response.config.url!) throw new NotFoundError(response.config.url!)
} }
throw e throw e
} }
private async parseResponse<T>(response: AxiosResponse<any>) { private parseResponse<T>(response: AxiosResponse<any>) {
const etag = response?.headers ? response.headers['etag'] : '' const etag = response?.headers ? response.headers['etag'] : ''
let parsedResponse let parsedResponse
let includeSAS9Log: boolean = false let includeSAS9Log: boolean = false