1
0
mirror of https://github.com/sasjs/adapter.git synced 2026-01-15 16:10:06 +00:00

test(RequestClient): fix error handling

This commit is contained in:
Yury Shkoda
2021-12-21 11:40:59 +03:00
parent 2ebd6e11ba
commit 4197ad5aa8
3 changed files with 21 additions and 7 deletions

View File

@@ -215,6 +215,7 @@ export class RequestClient implements HttpClient {
.post<T>(url, data, { headers, withCredentials: true }) .post<T>(url, data, { headers, withCredentials: true })
.then((response) => { .then((response) => {
throwIfError(response) throwIfError(response)
return this.parseResponse<T>(response) return this.parseResponse<T>(response)
}) })
.catch(async (e) => { .catch(async (e) => {
@@ -464,6 +465,8 @@ export class RequestClient implements HttpClient {
if (e instanceof LoginRequiredError) { if (e instanceof LoginRequiredError) {
this.clearCsrfTokens() this.clearCsrfTokens()
throw e
} }
if (response?.status === 403 || response?.status === 449) { if (response?.status === 403 || response?.status === 449) {
@@ -486,7 +489,8 @@ export class RequestClient implements HttpClient {
else return else return
} }
throw prefixMessage(e, 'Error while handling error. ') if (e.message) throw e
else throw prefixMessage(e, 'Error while handling error. ')
} }
protected parseResponse<T>(response: AxiosResponse<any>) { protected parseResponse<T>(response: AxiosResponse<any>) {
@@ -538,8 +542,9 @@ export class RequestClient implements HttpClient {
this.httpClient = createAxiosInstance(baseUrl, httpsAgent) this.httpClient = createAxiosInstance(baseUrl, httpsAgent)
this.httpClient.defaults.validateStatus = (status) => this.httpClient.defaults.validateStatus = (status) => {
status >= 200 && status < 401 return status >= 200 && status <= 401
}
} }
} }

View File

@@ -5,6 +5,8 @@ import { app, mockedAuthResponse } from './SAS_server_app'
import { ServerType } from '@sasjs/utils' import { ServerType } from '@sasjs/utils'
import SASjs from '../SASjs' import SASjs from '../SASjs'
import * as axiosModules from '../utils/createAxiosInstance' import * as axiosModules from '../utils/createAxiosInstance'
import { LoginRequiredError } from '../types/errors'
import { prefixMessage } from '@sasjs/utils/error'
const axiosActual = jest.requireActual('axios') const axiosActual = jest.requireActual('axios')
@@ -55,8 +57,11 @@ describe('RequestClient', () => {
it('should response the POST method with Unauthorized', async () => { it('should response the POST method with Unauthorized', async () => {
await expect( await expect(
adapter.getAccessToken('clientId', 'clientSecret', 'incorrect') adapter.getAccessToken('clientId', 'clientSecret', 'incorrect')
).rejects.toThrow( ).rejects.toEqual(
'Error while getting access token. Request failed with status code 401' prefixMessage(
new LoginRequiredError(),
'Error while getting access token. '
)
) )
}) })
}) })
@@ -132,8 +137,11 @@ describe('RequestClient - Self Signed Server', () => {
it('should response the POST method with Unauthorized', async () => { it('should response the POST method with Unauthorized', async () => {
await expect( await expect(
adapter.getAccessToken('clientId', 'clientSecret', 'incorrect') adapter.getAccessToken('clientId', 'clientSecret', 'incorrect')
).rejects.toThrow( ).rejects.toEqual(
'Error while getting access token. Request failed with status code 401' prefixMessage(
new LoginRequiredError(),
'Error while getting access token. '
)
) )
}) })
}) })

View File

@@ -18,6 +18,7 @@ app.get('/', function (req: any, res: any) {
app.post('/SASLogon/oauth/token', function (req: any, res: any) { app.post('/SASLogon/oauth/token', function (req: any, res: any) {
let valid = true let valid = true
// capture the encoded form data // capture the encoded form data
req.on('data', (data: any) => { req.on('data', (data: any) => {
const resData = data.toString() const resData = data.toString()