Skip to content

Commit 6473d91

Browse files
authored
fix: Await super request response instead of returning the promise (#2484)
* fix: Await super request response instead of returning the promise * test: Add test checking the name already in use error message
1 parent 4f45abb commit 6473d91

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

src/lib/api/builder.spec.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { AxiosError, AxiosRequestConfig } from 'axios'
2+
import { BaseAPI } from 'decentraland-dapps/dist/lib/api'
3+
import { RootStore } from 'modules/common/types'
4+
import { Authorization } from './auth'
5+
import { BuilderAPI } from './builder'
6+
7+
jest.mock('./auth')
8+
9+
const mockUrl = 'https://mock.url.xyz'
10+
const mockAuthorization: Authorization = new Authorization({} as RootStore)
11+
const mockBuilder = new BuilderAPI(mockUrl, mockAuthorization)
12+
13+
describe('when making a request to the builder server', () => {
14+
describe('when the request fails', () => {
15+
describe('and the error is an Axios Error', () => {
16+
let error: AxiosError
17+
18+
beforeEach(() => {
19+
error = {
20+
name: 'Axios Error',
21+
message: 'Error Message',
22+
config: {} as AxiosRequestConfig,
23+
code: '409',
24+
response: {
25+
data: { id: 'id-with-error', error: 'Name already in use' },
26+
status: 409,
27+
statusText: 'Conflict',
28+
headers: {},
29+
config: {}
30+
},
31+
isAxiosError: true,
32+
toJSON: jest.fn()
33+
}
34+
jest.spyOn(BaseAPI.prototype, 'request').mockRejectedValue(error)
35+
})
36+
37+
it('should store the response data error inside the error message', async () => {
38+
return expect(mockBuilder.request('GET', '/')).rejects.toEqual({ ...error, message: error.response?.data.error })
39+
})
40+
})
41+
})
42+
})

src/lib/api/builder.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,9 @@ export class BuilderAPI extends BaseAPI {
536536
authConfig = { ...authConfig, headers }
537537

538538
try {
539-
return super.request(method, path, params, authConfig)
539+
const response: any = await super.request(method, path, params, authConfig)
540+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
541+
return response
540542
} catch (error) {
541543
if (this.isAxiosError(error) && error.response) {
542544
error.message = error.response.data.error

0 commit comments

Comments
 (0)