Skip to content

Commit

Permalink
Merge pull request #106 from saucelabs/devx-502-show-err-body
Browse files Browse the repository at this point in the history
Show err response body for createJob API
  • Loading branch information
tianfeng92 authored Jan 27, 2021
2 parents e111577 + 5f20c03 commit c3be29c
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
10 changes: 10 additions & 0 deletions apis/testrunner.json
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,16 @@
}
}
}
},
"422": {
"description": "invalid parameters",
"content": {
"text/plain": {
"schema": {
"type": "string"
}
}
}
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,9 @@ export default class SauceLabs {
),
responseType: 'json'
})

return response.body
} catch (err) {
throw new Error(`Failed calling ${propName}: ${err.message}`)
throw new Error(`Failed calling ${propName}: ${err.message}, ${err.response && err.response.body}`)
}
}
}
Expand Down
1 change: 1 addition & 0 deletions tests/__mocks__/got.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const gotMock = jest.fn()
.mockImplementation(() => Promise.resolve({ statusCode: 200, headers }))
gotMock.get = gotMock
gotMock.put = gotMock
gotMock.post = gotMock
gotMock.extend = jest.fn().mockReturnValue(gotMock)
gotMock.setHeader = (header) => (headers = header)

Expand Down
14 changes: 13 additions & 1 deletion tests/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ test('should handle error case', async () => {
limit: 123,
full: true
}).catch((err) => err)
expect(error.message).toBe('Failed calling listJobs: Not Found')
expect(error.message).toContain('Failed calling listJobs: Not Found')
})

test('should be able to download assets', async () => {
Expand Down Expand Up @@ -454,6 +454,18 @@ describe('startSauceConnect', () => {
})
})

test('should output failure msg for createJob API', async () => {
const response = new Error('Response code 422 (Unprocessable Entity)')
response.statusCode = 422
response.response = {body: 'empty framework'}
got.post.mockReturnValue(Promise.reject(response))

const api = new SauceLabs({ user: 'foo', key: 'bar' })
const error = await api.createJob({framework: ''}).catch((err) => err)

expect(error.message).toBe('Failed calling createJob: Response code 422 (Unprocessable Entity), empty framework')
})

afterEach(() => {
fs.writeFileSync.mockClear()
got.mockClear()
Expand Down

0 comments on commit c3be29c

Please sign in to comment.