Repository: saucelabs/node-saucelabs
Pull request #106 (2021-01-21 21:58:13)

Show err response body for createJob API
For `createJob` API, it explains why it failed in response body. But when calling this API in node-saucelabs, it only outputs `err.message`. It's not enough to figure out which part is wrong with current implementation. I only added `err.response.body` for `createJob` API cause it will be deprecated in the future. Hope this change doesn't affect other APIs.
Before:
```
 ./bin/sl createJob '{}'
Error: Failed calling createJob: Response code 422 (Unprocessable Entity)
```
After:
```
 ./bin/sl createJob '{}'
Error: Failed calling createJob: Response code 422 (Unprocessable Entity), empty framework
```
Other APIs:
```
./bin/sl updateJob cb-onboarding xxxxx "{}"
Error: Failed calling updateJob: Response code 404 (Not Found)
```


Patch:
diff --git a/src/index.js b/src/index.js
index a661d06..9703cf2 100644
--- a/src/index.js
+++ b/src/index.js
@@ -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}`)
             }
         }
     }


Test patch:
diff --git a/apis/testrunner.json b/apis/testrunner.json
index 6bf8538..07bbef0 100644
--- a/apis/testrunner.json
+++ b/apis/testrunner.json
@@ -168,6 +168,16 @@
                 }
               }
             }
+          },
+          "422": {
+            "description": "invalid parameters",
+            "content": {
+              "text/plain": {
+                "schema": {
+                  "type": "string"
+                }
+              }
+            }
           }
         }
       }
diff --git a/tests/__mocks__/got.js b/tests/__mocks__/got.js
index ab33c8a..b949687 100644
--- a/tests/__mocks__/got.js
+++ b/tests/__mocks__/got.js
@@ -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)
 
diff --git a/tests/index.test.js b/tests/index.test.js
index b1d368a..98c5f29 100644
--- a/tests/index.test.js
+++ b/tests/index.test.js
@@ -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 () => {
@@ -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()