Skip to content

Commit

Permalink
feat(StreamLabs): Log API errors
Browse files Browse the repository at this point in the history
  • Loading branch information
orestes committed Apr 13, 2020
1 parent 4a98b4d commit f620b37
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/services/StreamLabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,20 @@ class StreamLabs {
this.logger = logger;
}

alert({ message }) {
return axios.post(`${this.base}/alerts`, {
access_token: this.token,
message,
type: 'follow',
});
async alert({ message }) {
try {
await axios.post(`${this.base}/alerts`, {
access_token: this.token,
message,
type: 'follow',
});
} catch (error) {
if (this.logger) {
this.logger.log('error', 'StreamLabs API error', error);
}

throw error;
}
}
}

Expand Down
28 changes: 28 additions & 0 deletions test/services/StreamLabs.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,34 @@ describe('StreamLabs', () => {
);
});

it('logs the response body for failed HTTP 401 requests', async () => {
axiosSpy.mockClear(); // remove previous mock
const mockAxiosErrorResponse = {
response: { body: 'Reason for error response', status: 401 },
};
axiosSpy.mockImplementationOnce(
jest.fn().mockRejectedValue(mockAxiosErrorResponse),
);

await axios.post(); // should reject with the mocked response

const spyLogger = { log: jest.fn() };
const config = {
token: 'fake-token',
};

const subject = new StreamLabs(config, spyLogger);

await expect(subject.alert({ message: 'hello' })).rejects.toEqual(
expect.any(Object),
);
expect(spyLogger.log).toHaveBeenLastCalledWith(
'error',
'StreamLabs API error',
mockAxiosErrorResponse,
);
});

it("uses the text given as an argument as message to 'StreamLabs'", async () => {
const subject = new StreamLabs({});

Expand Down

0 comments on commit f620b37

Please sign in to comment.