1
0
mirror of https://github.com/sasjs/adapter.git synced 2025-12-11 01:14:36 +00:00

fix(error-handling): added InternalServerError

This commit is contained in:
Yury Shkoda
2021-03-10 14:43:47 +03:00
parent 599c130395
commit 719135e366
16 changed files with 76 additions and 34 deletions

View File

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

View File

@@ -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(

View File

@@ -1,5 +1,6 @@
import SASjs from './SASjs'
export * from './types'
export * from './types/errors'
export * from './SASViyaApiClient'
export * from './SAS9ApiClient'
export default SASjs

View File

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

View File

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

View File

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

View File

@@ -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,15 +102,18 @@ export class RequestClient implements HttpClient {
return this.parseResponse<T>(response)
})
.catch(async (e) => {
return await this.handleError(e, () =>
this.get<T>(url, accessToken, contentType, overrideHeaders).catch(
(err) => {
throw prefixMessage(
err,
'Error while executing handle error callback. '
)
}
)
return await this.handleError(
e,
() =>
this.get<T>(url, accessToken, contentType, overrideHeaders).catch(
(err) => {
throw prefixMessage(
err,
'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
}

View File

@@ -1,4 +1,4 @@
import { Job } from './Job'
import { Job } from '../Job'
export class ComputeJobExecutionError extends Error {
constructor(public job: Job, public log: string) {

View File

@@ -0,0 +1,9 @@
export class InternalServerError extends Error {
constructor() {
super('Error: Internal server error.')
this.name = 'InternalServerError'
Object.setPrototypeOf(this, InternalServerError.prototype)
}
}

View 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'

View File

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