diff --git a/src/job-execution/WebJobExecutor.ts b/src/job-execution/WebJobExecutor.ts index b3dc130..7374ceb 100644 --- a/src/job-execution/WebJobExecutor.ts +++ b/src/job-execution/WebJobExecutor.ts @@ -172,7 +172,7 @@ export class WebJobExecutor extends BaseJobExecutor { const resObj = res if (this.serverType === ServerType.Sasjs) { - if (res.result._webout < 1) + if (res.result._webout.length < 1) throw new JobExecutionError( 0, 'Job execution failed', diff --git a/src/request/RequestClient.ts b/src/request/RequestClient.ts index 87bcee1..81c7517 100644 --- a/src/request/RequestClient.ts +++ b/src/request/RequestClient.ts @@ -19,6 +19,7 @@ import { parseSourceCode, createAxiosInstance } from '../utils' +import { InvalidCsrfError } from '../types/errors/InvalidCsrfError' export interface HttpClient { get( @@ -498,6 +499,24 @@ export class RequestClient implements HttpClient { throw e } + if (e instanceof InvalidCsrfError) { + // Fetching root will inject CSRF token in cookie + await this.httpClient + .get('/', { + withCredentials: true + }) + .catch((err) => { + throw prefixMessage(err, 'Error while re-fetching CSRF token.') + }) + + return await callback().catch((err: any) => { + throw prefixMessage( + err, + 'Error while executing callback in handleError. ' + ) + }) + } + if (response?.status === 403 || response?.status === 449) { this.parseAndSetCsrfToken(response) @@ -584,9 +603,17 @@ export class RequestClient implements HttpClient { export const throwIfError = (response: AxiosResponse) => { switch (response.status) { case 400: - if (typeof response.data === 'object') { + if ( + typeof response.data === 'object' && + response.data.error === 'invalid_grant' + ) { + // In SASVIYA when trying to get access token, if auth code is wrong status code will be 400 so in such case we return login required error. throw new LoginRequiredError(response.data) } + + if (response.data.toLowerCase() === 'invalid csrf token!') { + throw new InvalidCsrfError() + } break case 401: if (typeof response.data === 'object') { diff --git a/src/types/errors/InvalidCsrfError.ts b/src/types/errors/InvalidCsrfError.ts new file mode 100644 index 0000000..cf174ab --- /dev/null +++ b/src/types/errors/InvalidCsrfError.ts @@ -0,0 +1,9 @@ +export class InvalidCsrfError extends Error { + constructor() { + const message = 'Invalid CSRF token!' + + super(`Auth error: ${message}`) + this.name = 'InvalidCsrfError' + Object.setPrototypeOf(this, InvalidCsrfError.prototype) + } +}