1
0
mirror of https://github.com/sasjs/adapter.git synced 2025-12-11 09:24:35 +00:00

Compare commits

...

11 Commits

Author SHA1 Message Date
a54df1e2cb fix: make ErrorBody interface public
So it could be used as a type in client
2022-03-18 17:12:49 +01:00
Allan Bowe
804e78cf0c Merge pull request #678 from sasjs/issue-677
fix: return requestPromise from sas9JobExecutor
2022-03-11 12:54:17 +02:00
f6a621fe46 chore: update comments 2022-03-11 14:22:45 +05:00
c47d0c9789 fix: return requestPromise from sas9JobExecutor #677 2022-03-11 14:16:44 +05:00
Allan Bowe
1ddc71b017 Create CNAME 2022-03-10 12:08:46 +00:00
Allan Bowe
3e507885ab Merge pull request #676 from sasjs/issue-675
fix: no need to stringify res/err in sas9JobExecutor before appending…
2022-03-10 14:06:50 +02:00
e92d0d73b5 chore: add comments 2022-03-10 16:17:09 +05:00
00a99e752c fix: no need to stringify res/err in sas9JobExecutor before appending request #675 2022-03-10 15:44:38 +05:00
Allan Bowe
b13f3d2fcb Merge pull request #667 from sasjs/sas-viya-auth-code-responses
fix: raising error with details we get from server
2022-03-09 11:34:40 +02:00
Saad Jutt
495e4b9069 test: updated RequestClient specs 2022-03-02 20:42:14 +05:00
Saad Jutt
2e843e3f36 fix: raising error with details we get from server 2022-03-02 20:25:56 +05:00
7 changed files with 50 additions and 35 deletions

1
docs/CNAME Normal file
View File

@@ -0,0 +1 @@
adapter.sasjs.io

View File

@@ -74,38 +74,32 @@ export class Sas9JobExecutor extends BaseJobExecutor {
? 'multipart/form-data; boundary=' + (formData as any)._boundary
: 'text/plain'
return await this.sas9RequestClient!.post(
apiUrl,
formData,
undefined,
contentType,
{
const requestPromise = new Promise((resolve, reject) =>
this.sas9RequestClient!.post(apiUrl, formData, undefined, contentType, {
Accept: '*/*',
Connection: 'Keep-Alive'
}
})
.then((res: any) => {
// appending response to requests array that will be used for requests history reference
this.requestClient!.appendRequest(res, sasJob, config.debug)
resolve(res)
})
.catch((err: any) => {
// by default error string is equal to actual error object
let errString = err
// if error object contains non empty result attribute, set result to errString
if (err.result && err.result !== '') errString = err.result
// if there's no result but error message, set error message to errString
else if (err.message) errString = err.message
// appending error to requests array that will be used for requests history reference
this.requestClient!.appendRequest(errString, sasJob, config.debug)
reject(new ErrorResponse(err?.message, err))
})
)
.then((res: any) => {
let resString = res
if (typeof res === 'object') {
resString = JSON.stringify(res)
}
this.requestClient!.appendRequest(resString, sasJob, config.debug)
return res
})
.catch((err: any) => {
let errString = err
if (typeof err === 'object') {
errString = JSON.stringify(errString)
}
this.requestClient!.appendRequest(errString, sasJob, config.debug)
return err
})
return requestPromise
}
private getRequestParams(config: any): any {

View File

@@ -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')) {

View File

@@ -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. '
)
)

View File

@@ -21,7 +21,7 @@ export class ErrorResponse {
}
}
interface ErrorBody {
export interface ErrorBody {
message: string
details: string
raw: any

View File

@@ -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)
}

View File

@@ -11,3 +11,4 @@ export * from './RootFolderNotFoundError'
export * from './JsonParseArrayError'
export * from './WeboutResponseError'
export * from './InvalidJsonError'
export * from './ErrorResponse'