mirror of
https://github.com/sasjs/adapter.git
synced 2025-12-11 09:24:35 +00:00
fix(error-handling): added InternalServerError
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { isUrl } from './utils'
|
||||
import { UploadFile } from './types/UploadFile'
|
||||
import { ErrorResponse, LoginRequiredError } from './types'
|
||||
import { ErrorResponse, LoginRequiredError } from './types/errors'
|
||||
import { RequestClient } from './request/RequestClient'
|
||||
|
||||
export class FileUploader {
|
||||
|
||||
@@ -8,10 +8,13 @@ import {
|
||||
Folder,
|
||||
EditContextInput,
|
||||
JobDefinition,
|
||||
PollOptions,
|
||||
ComputeJobExecutionError,
|
||||
JobExecutionError
|
||||
PollOptions
|
||||
} from './types'
|
||||
import {
|
||||
ComputeJobExecutionError,
|
||||
JobExecutionError,
|
||||
NotFoundError
|
||||
} from './types/errors'
|
||||
import { formatDataForRequest } from './utils/formatDataForRequest'
|
||||
import { SessionManager } from './SessionManager'
|
||||
import { ContextManager } from './ContextManager'
|
||||
@@ -19,7 +22,6 @@ import { timestampToYYYYMMDDHHMMSS } from '@sasjs/utils/time'
|
||||
import { Logger, LogLevel } from '@sasjs/utils/logger'
|
||||
import { isAuthorizeFormRequired } from './auth/isAuthorizeFormRequired'
|
||||
import { RequestClient } from './request/RequestClient'
|
||||
import { NotFoundError } from './types/NotFoundError'
|
||||
import { SasAuthResponse } from '@sasjs/utils/types'
|
||||
import { prefixMessage } from '@sasjs/utils/error'
|
||||
|
||||
@@ -1082,7 +1084,9 @@ export class SASViyaApiClient {
|
||||
.get<string>(
|
||||
`${this.serverUrl}${stateLink.href}?_action=wait&wait=30`,
|
||||
accessToken,
|
||||
'text/plain'
|
||||
'text/plain',
|
||||
{},
|
||||
this.debug
|
||||
)
|
||||
.catch((err) => {
|
||||
throw prefixMessage(err, 'Error while getting job state. ')
|
||||
@@ -1107,7 +1111,9 @@ export class SASViyaApiClient {
|
||||
.get<string>(
|
||||
`${this.serverUrl}${stateLink.href}?_action=wait&wait=30`,
|
||||
accessToken,
|
||||
'text/plain'
|
||||
'text/plain',
|
||||
{},
|
||||
this.debug
|
||||
)
|
||||
.catch((err) => {
|
||||
throw prefixMessage(
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import SASjs from './SASjs'
|
||||
export * from './types'
|
||||
export * from './types/errors'
|
||||
export * from './SASViyaApiClient'
|
||||
export * from './SAS9ApiClient'
|
||||
export default SASjs
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
import { ServerType } from '@sasjs/utils/types'
|
||||
import { ErrorResponse } from '..'
|
||||
import { SASViyaApiClient } from '../SASViyaApiClient'
|
||||
import { ComputeJobExecutionError, LoginRequiredError } from '../types'
|
||||
import {
|
||||
ErrorResponse,
|
||||
ComputeJobExecutionError,
|
||||
LoginRequiredError
|
||||
} from '../types/errors'
|
||||
import { BaseJobExecutor } from './JobExecutor'
|
||||
|
||||
export class ComputeJobExecutor extends BaseJobExecutor {
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
import { ServerType } from '@sasjs/utils/types'
|
||||
import { ErrorResponse } from '..'
|
||||
import { SASViyaApiClient } from '../SASViyaApiClient'
|
||||
import { JobExecutionError, LoginRequiredError } from '../types'
|
||||
import {
|
||||
ErrorResponse,
|
||||
JobExecutionError,
|
||||
LoginRequiredError
|
||||
} from '../types/errors'
|
||||
import { BaseJobExecutor } from './JobExecutor'
|
||||
|
||||
export class JesJobExecutor extends BaseJobExecutor {
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
import { ServerType } from '@sasjs/utils/types'
|
||||
import { ErrorResponse, JobExecutionError, LoginRequiredError } from '..'
|
||||
import {
|
||||
ErrorResponse,
|
||||
JobExecutionError,
|
||||
LoginRequiredError
|
||||
} from '../types/errors'
|
||||
import { generateFileUploadForm } from '../file/generateFileUploadForm'
|
||||
import { generateTableUploadForm } from '../file/generateTableUploadForm'
|
||||
import { RequestClient } from '../request/RequestClient'
|
||||
|
||||
@@ -1,9 +1,13 @@
|
||||
import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios'
|
||||
import { CsrfToken, JobExecutionError } from '..'
|
||||
import { CsrfToken } from '..'
|
||||
import { isAuthorizeFormRequired, isLogInRequired } from '../auth'
|
||||
import { LoginRequiredError } from '../types'
|
||||
import { AuthorizeError } from '../types/AuthorizeError'
|
||||
import { NotFoundError } from '../types/NotFoundError'
|
||||
import {
|
||||
AuthorizeError,
|
||||
LoginRequiredError,
|
||||
NotFoundError,
|
||||
InternalServerError,
|
||||
JobExecutionError
|
||||
} from '../types/errors'
|
||||
import { parseWeboutResponse } from '../utils/parseWeboutResponse'
|
||||
import { prefixMessage } from '@sasjs/utils/error'
|
||||
|
||||
@@ -73,7 +77,8 @@ export class RequestClient implements HttpClient {
|
||||
url: string,
|
||||
accessToken: string | undefined,
|
||||
contentType: string = 'application/json',
|
||||
overrideHeaders: { [key: string]: string | number } = {}
|
||||
overrideHeaders: { [key: string]: string | number } = {},
|
||||
debug: boolean = false
|
||||
): Promise<{ result: T; etag: string }> {
|
||||
const headers = {
|
||||
...this.getHeaders(accessToken, contentType),
|
||||
@@ -97,7 +102,9 @@ export class RequestClient implements HttpClient {
|
||||
return this.parseResponse<T>(response)
|
||||
})
|
||||
.catch(async (e) => {
|
||||
return await this.handleError(e, () =>
|
||||
return await this.handleError(
|
||||
e,
|
||||
() =>
|
||||
this.get<T>(url, accessToken, contentType, overrideHeaders).catch(
|
||||
(err) => {
|
||||
throw prefixMessage(
|
||||
@@ -105,7 +112,8 @@ export class RequestClient implements HttpClient {
|
||||
'Error while executing handle error callback. '
|
||||
)
|
||||
}
|
||||
)
|
||||
),
|
||||
debug
|
||||
).catch((err) => {
|
||||
throw prefixMessage(err, 'Error while handling error. ')
|
||||
})
|
||||
@@ -340,7 +348,11 @@ export class RequestClient implements HttpClient {
|
||||
}
|
||||
}
|
||||
|
||||
private handleError = async (e: any, callback: any) => {
|
||||
private handleError = async (
|
||||
e: any,
|
||||
callback: any,
|
||||
debug: boolean = false
|
||||
) => {
|
||||
const response = e.response as AxiosResponse
|
||||
|
||||
if (e instanceof AuthorizeError) {
|
||||
@@ -386,10 +398,11 @@ export class RequestClient implements HttpClient {
|
||||
throw e
|
||||
} else if (response?.status === 404) {
|
||||
throw new NotFoundError(response.config.url!)
|
||||
} else if (response?.status === 502) {
|
||||
if (debug) throw new InternalServerError()
|
||||
else return
|
||||
}
|
||||
|
||||
console.log(`[e]`, e)
|
||||
|
||||
throw e
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Job } from './Job'
|
||||
import { Job } from '../Job'
|
||||
|
||||
export class ComputeJobExecutionError extends Error {
|
||||
constructor(public job: Job, public log: string) {
|
||||
9
src/types/errors/InternalServerError.ts
Normal file
9
src/types/errors/InternalServerError.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
export class InternalServerError extends Error {
|
||||
constructor() {
|
||||
super('Error: Internal server error.')
|
||||
|
||||
this.name = 'InternalServerError'
|
||||
|
||||
Object.setPrototypeOf(this, InternalServerError.prototype)
|
||||
}
|
||||
}
|
||||
7
src/types/errors/index.ts
Normal file
7
src/types/errors/index.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
export * from './AuthorizeError'
|
||||
export * from './ComputeJobExecutionError'
|
||||
export * from './InternalServerError'
|
||||
export * from './JobExecutionError'
|
||||
export * from './LoginRequiredError'
|
||||
export * from './NotFoundError'
|
||||
export * from './ErrorResponse'
|
||||
@@ -1,14 +1,10 @@
|
||||
export * from './ComputeJobExecutionError'
|
||||
export * from './Context'
|
||||
export * from './CsrfToken'
|
||||
export * from './ErrorResponse'
|
||||
export * from './Folder'
|
||||
export * from './Job'
|
||||
export * from './JobExecutionError'
|
||||
export * from './JobDefinition'
|
||||
export * from './JobResult'
|
||||
export * from './Link'
|
||||
export * from './LoginRequiredError'
|
||||
export * from './SASjsConfig'
|
||||
export * from './SASjsRequest'
|
||||
export * from './Session'
|
||||
|
||||
Reference in New Issue
Block a user