Skip to content

Commit 38305db

Browse files
CyberT33NCyberT33N
CyberT33N
authored and
CyberT33N
committed
feat(CCS-001): changed try catch typescript logic
1 parent c304a21 commit 38305db

9 files changed

+236
-180
lines changed

README.md

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ import { BaseError } from 'error-manager-helper'
9393
try {
9494
const chatCompletion = await this.client.chat.completions.create(params)
9595
return chatCompletion
96-
} catch (err: unknown) {
97-
throw new BaseError('Can not create chat completion', err as Error)
96+
} catch (err) {
97+
throw new BaseError('Can not create chat completion', err as Error)
9898
}
9999
```
100100

@@ -116,7 +116,7 @@ import { HttpClientError } from 'error-manager-helper'
116116
try {
117117
const response = await axios.request(config)
118118
return response
119-
} catch (err: unknown) {
119+
} catch (err) {
120120
throw new HttpClientError('Can not send request', err as AxiosError)
121121
}
122122
```
@@ -248,22 +248,28 @@ _________________________________________
248248
## Integration Test
249249
- The example below demonstrates an integration test for an internal service that throws a `BaseError`.
250250
```typescript
251-
import { it, expect, expectTypeOf } from 'vitest'
251+
import axios, { AxiosError } from 'axios'
252+
import { it, expect, expectTypeOf, assert } from 'vitest'
252253
import { type IBaseError, StatusCodes } from 'error-manager-helper'
253254

254255
it('should return 500 with BaseError details - error passed', async() => {
255256
try {
256257
await axios.get('https://localhost:3000/base-error?param=wrong')
257-
throw new Error('Base Error Test - This line should not be called')
258-
} catch (e: unknown) {
259-
const { response } = e as AxiosError
260-
expect(response?.status).to.equal(StatusCodes.INTERNAL_SERVER_ERROR)
258+
assert.fail('This line should not be reached')
259+
} catch (err) {
260+
if (err instanceof AxiosError) {
261+
expect(err.status).to.equal(StatusCodes.INTERNAL_SERVER_ERROR)
261262

262-
const data = response?.data as IBaseError
263-
expectTypeOf(data).toEqualTypeOf<IBaseError>()
263+
const data: IBaseError = err.response?.data
264+
expectTypeOf(data).toEqualTypeOf<IBaseError>()
264265

265-
expect(data.error).toEqual(`Error: ${errorMessageFromService}`)
266-
expect(data.message).toBe(errorMessage)
266+
expect(data.error).toEqual(`Error: ${errorMessageFromService}`)
267+
expect(data.message).toBe(errorMessage)
268+
269+
return
270+
}
271+
272+
assert.fail('This line should not be reached')
267273
}
268274
})
269275
```
@@ -273,7 +279,7 @@ it('should return 500 with BaseError details - error passed', async() => {
273279

274280
## Unit Test
275281
```typescript
276-
import { it, expect, expectTypeOf } from 'vitest'
282+
import { it, expect, expectTypeOf, assert } from 'vitest'
277283
import { BaseError, type IBaseError } from 'error-manager-helper'
278284

279285
describe('Any test block', () => {
@@ -288,15 +294,20 @@ describe('Any test block', () => {
288294
it('should throw BaseError', () => {
289295
try {
290296
fn()
291-
throw new Error('Base Error Test - This line should not be called')
292-
} catch (err: unknown) {
293-
const typedErr = err as IBaseError
297+
assert.fail('This line should not be reached')
298+
} catch (err) {
299+
if (err instanceof BaseError) {
300+
const typedErr: BaseError = err
301+
302+
expectTypeOf(typedErr).toEqualTypeOf<IBaseError>()
303+
304+
expect(typedErr.error).toEqual(error)
305+
expect(typedErr.message).toBe(errMsg)
294306

295-
expectTypeOf(typedErr).toEqualTypeOf<IBaseError>()
296-
expect(typedErr).toBeInstanceOf(BaseError)
307+
return
308+
}
297309

298-
expect(typedErr.error).toEqual(error)
299-
expect(typedErr.message).toBe(errMsg)
310+
assert.fail('This line should not be reached')
300311
}
301312
})
302313
})

test/integration/pretestAll.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ export async function setup(): Promise<void> {
9090
app.get('/httpclient-error', async() => {
9191
try {
9292
await axios.get(`${BASE_URL}/notFound`)
93-
} catch (e: unknown) {
93+
} catch (e) {
9494
throw new HttpClientError(errorMessage, e as AxiosError)
9595
}
9696
})

test/integration/src/errors/BaseError.rest.test.ts

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
███████████████████████████████████████████████████████████████████████████████
1414
*/
1515

16-
import axios, { type AxiosError } from 'axios'
17-
import { describe, it, expect, expectTypeOf } from 'vitest'
16+
import axios, { AxiosError } from 'axios'
17+
import { describe, it, expect, expectTypeOf, assert } from 'vitest'
1818

1919
import type { IBaseError } from '@/src/errors/BaseError'
2020

@@ -29,42 +29,52 @@ describe('[INTEGRATION] - src/errors/BaseError', () => {
2929
it('should return 500 with BaseError details - error passed', async() => {
3030
try {
3131
await axios.get(`${BASE_URL}/base-error?error=true`)
32-
throw new Error('Base Error Test - This should not be called')
33-
} catch (e: unknown) {
34-
const { response } = e as AxiosError
35-
expect(response?.status).to.equal(StatusCodes.INTERNAL_SERVER_ERROR)
32+
assert.fail('This line should not be reached')
33+
} catch (err) {
34+
if (err instanceof AxiosError) {
35+
expect(err.status).to.equal(StatusCodes.INTERNAL_SERVER_ERROR)
3636

37-
const data = response?.data as IBaseError
38-
expectTypeOf(data).toEqualTypeOf<IBaseError>()
37+
const data: IBaseError = err.response?.data
38+
expectTypeOf(data).toEqualTypeOf<IBaseError>()
3939

40-
expect(data.message).to.equal(errorMessage)
41-
expect(data.environment).to.equal(process.env.npm_lifecycle_event)
42-
expect(data.name).to.equal(ErrorType.BASE)
43-
expect(data.error).to.equal(`Error: ${errorMessageOriginal}`)
44-
expect(data.httpStatus).to.equal(StatusCodes.INTERNAL_SERVER_ERROR)
45-
expect(data.timestamp).toBeDefined()
46-
expect(data.stack).toBeDefined()
40+
expect(data.message).to.equal(errorMessage)
41+
expect(data.environment).to.equal(process.env.npm_lifecycle_event)
42+
expect(data.name).to.equal(ErrorType.BASE)
43+
expect(data.error).to.equal(`Error: ${errorMessageOriginal}`)
44+
expect(data.httpStatus).to.equal(StatusCodes.INTERNAL_SERVER_ERROR)
45+
expect(data.timestamp).toBeDefined()
46+
expect(data.stack).toBeDefined()
47+
48+
return
49+
}
50+
51+
assert.fail('This line should not be reached')
4752
}
4853
})
4954

5055
it('should return 500 with BaseError details - no error passed', async() => {
5156
try {
5257
await axios.get(`${BASE_URL}/base-error`)
53-
throw new Error('Base Error Test - This should not be called')
54-
} catch (e: unknown) {
55-
const { response } = e as AxiosError
56-
expect(response?.status).to.equal(StatusCodes.INTERNAL_SERVER_ERROR)
58+
assert.fail('This line should not be reached')
59+
} catch (err) {
60+
if (err instanceof AxiosError) {
61+
expect(err.status).to.equal(StatusCodes.INTERNAL_SERVER_ERROR)
62+
63+
const data: IBaseError = err.response?.data
64+
expectTypeOf(data).toEqualTypeOf<IBaseError>()
65+
66+
expect(data.message).to.equal(errorMessage)
67+
expect(data.environment).to.equal(process.env.npm_lifecycle_event)
68+
expect(data.name).to.equal(ErrorType.BASE)
69+
expect(data.httpStatus).to.equal(StatusCodes.INTERNAL_SERVER_ERROR)
70+
expect(data.error).toBeUndefined()
71+
expect(data.timestamp).toBeDefined()
72+
expect(data.stack).toBeDefined()
5773

58-
const data = response?.data as IBaseError
59-
expectTypeOf(data).toEqualTypeOf<IBaseError>()
74+
return
75+
}
6076

61-
expect(data.message).to.equal(errorMessage)
62-
expect(data.environment).to.equal(process.env.npm_lifecycle_event)
63-
expect(data.name).to.equal(ErrorType.BASE)
64-
expect(data.httpStatus).to.equal(StatusCodes.INTERNAL_SERVER_ERROR)
65-
expect(data.error).toBeUndefined()
66-
expect(data.timestamp).toBeDefined()
67-
expect(data.stack).toBeDefined()
77+
assert.fail('This line should not be reached')
6878
}
6979
})
7080
})

test/integration/src/errors/HttpClientError.rest.test.ts

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
███████████████████████████████████████████████████████████████████████████████
1414
*/
1515

16-
import axios, { type AxiosError } from 'axios'
17-
import { describe, it, expect, expectTypeOf } from 'vitest'
16+
import axios, { AxiosError } from 'axios'
17+
import { describe, it, expect, expectTypeOf, assert } from 'vitest'
1818

1919
import type { IHttpClientError } from '@/src/errors/HttpClientError'
2020

@@ -29,28 +29,32 @@ describe('[INTEGRATION] - src/errors/HttpClientError', () => {
2929
it('should return 404 with HttpClientError details', async() => {
3030
try {
3131
await axios.get(`${BASE_URL}/httpclient-error`)
32-
throw new Error('HttpClient Error Test - This should not be called')
33-
} catch (e: unknown) {
34-
const { response } = e as AxiosError
35-
36-
expect(response?.status).to.equal(StatusCodes.NOT_FOUND)
37-
38-
const data = response?.data as IHttpClientError
39-
expectTypeOf(data).toEqualTypeOf<IHttpClientError>()
40-
41-
expect(data.message).toBe(errorMessage)
42-
expect(data.environment).toBe(process.env.npm_lifecycle_event)
43-
expect(data.name).toBe(ErrorType.HTTP_CLIENT)
44-
expect(data.error).toBe('AxiosError: Request failed with status code 404')
45-
expect(data.httpStatus).toBe(StatusCodes.NOT_FOUND)
46-
expect(data.timestamp).toBeDefined()
47-
expect(data.stack).toBeDefined()
48-
49-
expect(data.data.errorMessage).toBeDefined()
50-
expect(data.data.headers).toBeDefined()
51-
expect(data.data.method).to.be.equal('get')
52-
expect(data.data.responseData).toBeDefined()
53-
expect(data.data.url).toBe(BASE_URL + '/notFound')
32+
assert.fail('This line should not be reached')
33+
} catch (err) {
34+
if (err instanceof AxiosError) {
35+
expect(err.status).to.equal(StatusCodes.NOT_FOUND)
36+
37+
const data: IHttpClientError = err.response?.data
38+
expectTypeOf(data).toEqualTypeOf<IHttpClientError>()
39+
40+
expect(data.message).toBe(errorMessage)
41+
expect(data.environment).toBe(process.env.npm_lifecycle_event)
42+
expect(data.name).toBe(ErrorType.HTTP_CLIENT)
43+
expect(data.error).toBe('AxiosError: Request failed with status code 404')
44+
expect(data.httpStatus).toBe(StatusCodes.NOT_FOUND)
45+
expect(data.timestamp).toBeDefined()
46+
expect(data.stack).toBeDefined()
47+
48+
expect(data.data.errorMessage).toBeDefined()
49+
expect(data.data.headers).toBeDefined()
50+
expect(data.data.method).to.be.equal('get')
51+
expect(data.data.responseData).toBeDefined()
52+
expect(data.data.url).toBe(BASE_URL + '/notFound')
53+
54+
return
55+
}
56+
57+
assert.fail('This line should not be reached')
5458
}
5559
})
5660
})

test/integration/src/errors/ResourceNotFoundError.rest.test.ts

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
███████████████████████████████████████████████████████████████████████████████
1414
*/
1515

16-
import axios, { type AxiosError } from 'axios'
17-
import { describe, it, expect, expectTypeOf } from 'vitest'
16+
import axios, { AxiosError } from 'axios'
17+
import { describe, it, expect, expectTypeOf, assert } from 'vitest'
1818

1919
import type { IResourceNotFoundError } from '@/src/errors/ResourceNotFoundError'
2020

@@ -30,49 +30,57 @@ describe('[INTEGRATION] - src/errors/ResourceNotFoundError', () => {
3030
it('should return 404 with ResourceNotFoundError details - with error', async() => {
3131
try {
3232
await axios.get(`${BASE_URL}/resource-not-found?error=true`)
33-
throw new Error('Resource Error Test - This should not be called')
34-
} catch (e: unknown) {
35-
const { response } = e as AxiosError
33+
assert.fail('This line should not be reached')
34+
} catch (err) {
35+
if (err instanceof AxiosError) {
36+
expect(err.status).to.equal(StatusCodes.NOT_FOUND)
3637

37-
expect(response?.status).to.equal(StatusCodes.NOT_FOUND)
38+
const data: IResourceNotFoundError = err.response?.data
39+
expectTypeOf(data).toEqualTypeOf<IResourceNotFoundError>()
3840

39-
const data = response?.data as IResourceNotFoundError
40-
expectTypeOf(data).toEqualTypeOf<IResourceNotFoundError>()
41+
expect(data.message).to.equal(errorMessage)
42+
expect(data.environment).to.equal(process.env.npm_lifecycle_event)
43+
expect(data.name).to.equal(ErrorType.RESOURCE_NOT_FOUND)
44+
expect(data.error).to.equal(`Error: ${errorMessageOriginal}`)
45+
expect(data.httpStatus).to.equal(StatusCodes.NOT_FOUND)
4146

42-
expect(data.message).to.equal(errorMessage)
43-
expect(data.environment).to.equal(process.env.npm_lifecycle_event)
44-
expect(data.name).to.equal(ErrorType.RESOURCE_NOT_FOUND)
45-
expect(data.error).to.equal(`Error: ${errorMessageOriginal}`)
46-
expect(data.httpStatus).to.equal(StatusCodes.NOT_FOUND)
47+
expect(data.timestamp).toBeDefined()
48+
expect(data.stack).toBeDefined()
4749

48-
expect(data.timestamp).toBeDefined()
49-
expect(data.stack).toBeDefined()
50+
expect(data.data).to.be.deep.equal(errorData)
5051

51-
expect(data.data).to.be.deep.equal(errorData)
52+
return
53+
}
54+
55+
assert.fail('This line should not be reached')
5256
}
5357
})
5458

5559
it('should return 404 with ResourceNotFoundError details - without error', async() => {
5660
try {
5761
await axios.get(`${BASE_URL}/resource-not-found`)
58-
throw new Error('Resource Error Test - This should not be called')
59-
} catch (e: unknown) {
60-
const { response } = e as AxiosError
62+
assert.fail('This line should not be reached')
63+
} catch (err) {
64+
if (err instanceof AxiosError) {
65+
expect(err.status).to.equal(StatusCodes.NOT_FOUND)
66+
67+
const data: IResourceNotFoundError = err.response?.data
68+
expectTypeOf(data).toEqualTypeOf<IResourceNotFoundError>()
6169

62-
expect(response?.status).to.equal(StatusCodes.NOT_FOUND)
70+
expect(data.message).to.equal(errorMessage)
71+
expect(data.environment).to.equal(process.env.npm_lifecycle_event)
72+
expect(data.name).to.equal(ErrorType.RESOURCE_NOT_FOUND)
6373

64-
const data = response?.data as IResourceNotFoundError
65-
expectTypeOf(data).toEqualTypeOf<IResourceNotFoundError>()
74+
expect(data.stack).toBeDefined()
75+
expect(data.timestamp).toBeDefined()
76+
expect(data.error).toBeUndefined()
6677

67-
expect(data.message).to.equal(errorMessage)
68-
expect(data.environment).to.equal(process.env.npm_lifecycle_event)
69-
expect(data.name).to.equal(ErrorType.RESOURCE_NOT_FOUND)
78+
expect(data.data).to.be.deep.equal(errorData)
7079

71-
expect(data.stack).toBeDefined()
72-
expect(data.timestamp).toBeDefined()
73-
expect(data.error).toBeUndefined()
80+
return
81+
}
7482

75-
expect(data.data).to.be.deep.equal(errorData)
83+
assert.fail('This line should not be reached')
7684
}
7785
})
7886
})

0 commit comments

Comments
 (0)