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

chore(session-manager): improved error handling

This commit is contained in:
Yury Shkoda
2023-03-27 16:40:57 +03:00
parent 33bc8f92cb
commit 3d4c01622e
2 changed files with 38 additions and 33 deletions

View File

@@ -6,6 +6,10 @@ import { RequestClient } from './request/RequestClient'
const MAX_SESSION_COUNT = 1
interface ErrorResponse {
response: { status: number | string; data: { message: string } }
}
export class SessionManager {
private loggedErrors: NoSessionStateError[] = []
@@ -197,14 +201,10 @@ export class SessionManager {
const { result: createdSession, etag } = await this.requestClient
.post<Session>(url, {}, accessToken)
.catch((err) => {
.catch((err: ErrorResponse) => {
throw prefixMessage(
err,
`Error while creating session. ${this.getErrorMessage(
err,
url,
'POST'
)}`
this.getErrorMessage(err, url, 'POST'),
`Error while creating session. `
)
})
@@ -225,14 +225,10 @@ export class SessionManager {
.get<{
items: Context[]
}>(url, accessToken)
.catch((err) => {
.catch((err: ErrorResponse) => {
throw prefixMessage(
err,
`Error while getting list of contexts. ${this.getErrorMessage(
err,
url,
'GET'
)}`
this.getErrorMessage(err, url, 'GET'),
`Error while getting list of contexts. `
)
})
@@ -284,10 +280,7 @@ export class SessionManager {
const { result: state, responseStatus: responseStatus } =
await this.getSessionState(url, etag!, accessToken).catch((err) => {
throw prefixMessage(
this.getErrorMessage(err, url, 'GET'),
'Error while getting session state. '
)
throw prefixMessage(err, 'Error while waiting for session. ')
})
sessionState = state.trim()
@@ -353,15 +346,14 @@ export class SessionManager {
}
async getVariable(sessionId: string, variable: string, accessToken?: string) {
const url = `${this.serverUrl}/compute/sessions/${sessionId}/variables/${variable}`
return await this.requestClient
.get<SessionVariable>(
`${this.serverUrl}/compute/sessions/${sessionId}/variables/${variable}`,
accessToken
)
.get<SessionVariable>(url, accessToken)
.catch((err) => {
throw prefixMessage(
err,
`Error while fetching session variable '${variable}'.`
this.getErrorMessage(err, url, 'GET'),
`Error while fetching session variable '${variable}'. `
)
})
}

View File

@@ -3,7 +3,6 @@ import { RequestClient } from '../request/RequestClient'
import * as dotenv from 'dotenv'
import axios from 'axios'
import { Logger, LogLevel } from '@sasjs/utils'
import { prefixMessage } from '@sasjs/utils/error'
import { Session, Context } from '../types'
jest.mock('axios')
@@ -79,11 +78,11 @@ describe('SessionManager', () => {
})
)
const expectedError = `Error while fetching session variable '${testVariable}'.`
const expectedError = `Error while fetching session variable '${testVariable}'. GET request to ${process.env.SERVER_URL}/compute/sessions/testId/variables/${testVariable} failed with status code ${responseStatus}. ${responseErrorMessage}`
await expect(
sessionManager.getVariable('testId', testVariable)
).rejects.toEqual(prefixMessage({ response } as any, expectedError))
).rejects.toEqual(expectedError)
})
})
@@ -155,11 +154,25 @@ describe('SessionManager', () => {
})
it('should throw an error if could not get session state', async () => {
mockedAxios.get.mockImplementation(() => Promise.reject('Mocked error'))
const gettingSessionStatus = 500
const sessionStatusError = `Getting session status timed out after 60 seconds. Request failed with status code ${gettingSessionStatus}`
mockedAxios.get.mockImplementation(() =>
Promise.reject({
response: {
status: gettingSessionStatus,
data: {
message: sessionStatusError
}
}
})
)
const expectedError = `Error while waiting for session. Error while getting session state. GET request to ${process.env.SERVER_URL}?wait=30 failed with status code ${gettingSessionStatus}. ${sessionStatusError}`
await expect(
sessionManager['waitForSession'](session, null, 'access_token')
).rejects.toContain('Error while getting session state.')
).rejects.toEqual(expectedError)
})
it('should return session state', async () => {
@@ -196,13 +209,13 @@ describe('SessionManager', () => {
})
})
describe('removeSessionFromPull', () => {
it('should remove session from the pull of sessions', () => {
describe('removeSessionFromPool', () => {
it('should remove session from the pool of sessions', () => {
const session: Session = getMockSession()
const sessions: Session[] = [getMockSession(), session]
sessionManager['sessions'] = sessions
sessionManager['removeSessionFromPull'](session)
sessionManager['removeSessionFromPool'](session)
expect(sessionManager['sessions'].length).toEqual(1)
})
@@ -378,7 +391,7 @@ describe('SessionManager', () => {
sessionManager['currentContext'] = null
await expect(sessionManager['setCurrentContext']()).rejects.toEqual(
prefixMessage({ response } as any, expectedError)
expectedError
)
})