mirror of
https://github.com/sasjs/adapter.git
synced 2025-12-11 01:14:36 +00:00
Merge pull request #667 from sasjs/sas-viya-auth-code-responses
fix: raising error with details we get from server
This commit is contained in:
@@ -557,8 +557,18 @@ export class RequestClient implements HttpClient {
|
||||
}
|
||||
|
||||
export const throwIfError = (response: AxiosResponse) => {
|
||||
if (response.status === 401) {
|
||||
throw new LoginRequiredError()
|
||||
switch (response.status) {
|
||||
case 400:
|
||||
if (typeof response.data === 'object') {
|
||||
throw new LoginRequiredError(response.data)
|
||||
}
|
||||
break
|
||||
case 401:
|
||||
if (typeof response.data === 'object') {
|
||||
throw new LoginRequiredError(response.data)
|
||||
} else {
|
||||
throw new LoginRequiredError()
|
||||
}
|
||||
}
|
||||
|
||||
if (response.data?.entityID?.includes('login')) {
|
||||
|
||||
@@ -30,6 +30,11 @@ const ERROR_MESSAGES = {
|
||||
CCA: 'unable to verify the first certificate'
|
||||
}
|
||||
|
||||
const incorrectAuthCodeErr = {
|
||||
error: 'unauthorized',
|
||||
error_description: 'Bad credentials'
|
||||
}
|
||||
|
||||
describe('RequestClient', () => {
|
||||
let server: http.Server
|
||||
|
||||
@@ -65,7 +70,7 @@ describe('RequestClient', () => {
|
||||
adapter.getAccessToken('clientId', 'clientSecret', 'incorrect')
|
||||
).rejects.toEqual(
|
||||
prefixMessage(
|
||||
new LoginRequiredError(),
|
||||
new LoginRequiredError(incorrectAuthCodeErr),
|
||||
'Error while getting access token. '
|
||||
)
|
||||
)
|
||||
@@ -246,7 +251,7 @@ describe('RequestClient - Self Signed Server', () => {
|
||||
adapter.getAccessToken('clientId', 'clientSecret', 'incorrect')
|
||||
).rejects.toEqual(
|
||||
prefixMessage(
|
||||
new LoginRequiredError(),
|
||||
new LoginRequiredError(incorrectAuthCodeErr),
|
||||
'Error while getting access token. '
|
||||
)
|
||||
)
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
export class LoginRequiredError extends Error {
|
||||
constructor() {
|
||||
super('Auth error: You must be logged in to access this resource')
|
||||
constructor(details?: any) {
|
||||
const message = details
|
||||
? JSON.stringify(details, null, 2)
|
||||
: 'You must be logged in to access this resource'
|
||||
|
||||
super(`Auth error: ${message}`)
|
||||
this.name = 'LoginRequiredError'
|
||||
Object.setPrototypeOf(this, LoginRequiredError.prototype)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user