Skip to content

feat: Add logs for StreamLabs API errors #47

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/routes/github/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ const routes = (config) => [
const {
repository: { full_name: repositoryFullName },
} = payload;
const streamlabs = new StreamLabs({ token: config.STREAMLABS_TOKEN });
const streamlabs = new StreamLabs(
{ token: config.STREAMLABS_TOKEN },
request,
);

if (
event === 'ping' &&
Expand Down
6 changes: 3 additions & 3 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ const initServer = async (config) => {
plugin: laabr,
options: {
formats: {
'request': 'error.json',
'request-error': 'error.stackjson',
'uncaught': 'error.stackjson',
'request': '{ timestamp::time, level::level, tags::tags, message::message }',
'request-error': '{ timestamp::time, level::level, tags::tags, message::message, error::error, environment::environment, stack::error[stack] }',
'uncaught': '{ timestamp::time, level::level, tags::tags, message::message, error::error, environment::environment, stack::error[stack] }',
},
},
});
Expand Down
25 changes: 17 additions & 8 deletions src/services/StreamLabs.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
const axios = require('axios');
const axios = require('axios').default;

class StreamLabs {
constructor({ token }) {
constructor({ token }, logger) {
this.base = 'https://streamlabs.com/api/v1.0';
this.token = token;
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'], { data: error.response.data });
}

throw error;
}
}
}

Expand Down
47 changes: 45 additions & 2 deletions test/services/StreamLabs.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,28 @@ const axios = require('axios');
const { StreamLabs } = require('../../src/services/StreamLabs');

describe('StreamLabs', () => {
describe('#constructor', () => {
it('takes an optional logger argument', () => {
const subject = new StreamLabs({ token: '' }, { log: jest.fn() });

expect(subject).toBeInstanceOf(StreamLabs);
});
});
describe('#alert', () => {
let axiosSpy;

beforeEach(() => {
jest.restoreAllMocks();
const setupDefaultAxiosSpy = () => {
axiosSpy = jest.spyOn(axios, 'post');
axiosSpy.mockImplementationOnce(() => {});
};

afterEach(() => {
jest.restoreAllMocks();
});

it("uses axios to perform a 'POST' to the StreamLabs url", async () => {
setupDefaultAxiosSpy();

const config = {
token: 'token',
};
Expand All @@ -26,6 +38,8 @@ describe('StreamLabs', () => {
});

it("uses the given token as 'access_token'", async () => {
setupDefaultAxiosSpy();

const config = {
token: 'token',
};
Expand All @@ -39,7 +53,34 @@ describe('StreamLabs', () => {
);
});

it('logs the response body for failed HTTP 401 requests', async () => {
axiosSpy = jest.spyOn(axios, 'post');
const mockAxiosErrorResponse = {
response: { data: 'Reason for error response', status: 401 },
};
axiosSpy.mockImplementationOnce(
jest.fn().mockRejectedValue(mockAxiosErrorResponse),
);

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'],
expect.objectContaining({ data: mockAxiosErrorResponse.response.data }),
);
});

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

const subject = new StreamLabs({});

await subject.alert({ message: 'alert' });
Expand All @@ -51,6 +92,8 @@ describe('StreamLabs', () => {
});

it("sends the alerts with the type 'follow'", async () => {
setupDefaultAxiosSpy();

const subject = new StreamLabs({});

await subject.alert({ message: 'alert' });
Expand Down