Skip to content

Commit

Permalink
Added error handling to be sent to debug user account (#71)
Browse files Browse the repository at this point in the history
* Added error handling to be sent to debug user account

* updated lint

* add date
  • Loading branch information
amthorn authored Jun 6, 2021
1 parent 2f759b2 commit fb05e52
Show file tree
Hide file tree
Showing 9 changed files with 6,979 additions and 6,801 deletions.
12 changes: 8 additions & 4 deletions bin/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@
msg: >
Release Date: '{{ QUTEX_RELEASE_DATE }}'
Version: '{{ QUTEX_VERSION }}'
Image: 'docker.pkg.github.com/amthorn/qutex/qutex_bot:{{ QUTEX_VERSION }}'
- name: Confirm?
pause:
prompt: Press return to continue. Press Ctrl+c and then "a" to abort
Image: 'docker.pkg.github.com/amthorn/qutex/qutex_bot:{{ QUTEX_VERSION }}
- name: Copy file with owner and permission, using symbolic representation
ansible.builtin.copy:
src: ../docker-compose.yml
dest: '{{ SRC }}/docker-compose.yml'
owner: centos
group: centos
mode: u=rw,g=r,o=r
- name: Do deployment
environment:
QUTEX_RELEASE_DATE: "{{ QUTEX_RELEASE_DATE }}"
Expand Down
4 changes: 1 addition & 3 deletions bin/hosts.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
all:
hosts:
qutex:
ansible_host: 54.209.243.121
ansible_ssh_private_key_file: ~/.ssh/LightsailDefaultKey-us-east-1.pem
ansible_user: centos
ansible_host: qutex
ansible_become: yes

3 changes: 2 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ services:
RELEASE_DATE: ${QUTEX_RELEASE_DATE}
AUTHOR_NAME: Ava Thorn
AUTHOR_EMAIL: avatheavian@gmail.com
DEBUG_EMAIL: avthorn@cisco.com
SUPER_ADMINS: '["Y2lzY29zcGFyazovL3VzL1BFT1BMRS9kODRkZjI1MS1iYmY3LTRlZTEtOTM1OS00Y2I0MGIyOTBhN2I"]'
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_DATABASE: qutex
Expand Down Expand Up @@ -55,4 +56,4 @@ secrets:
mongoPassword:
file: mongoPassword
volumes:
mongo_volume:
mongo_volume:
1 change: 1 addition & 0 deletions services/bot/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"@types/mock-fs": "^4.13.0",
"@types/mongoose": "^5.10.5",
"@types/node": "^15.6.1",
"@types/uuid": "^8.3.0",
"@typescript-eslint/eslint-plugin": "^4.25.0",
"@typescript-eslint/parser": "^4.25.0",
"babel-jest": "^26.0.22",
Expand Down
12 changes: 11 additions & 1 deletion services/bot/src/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { Parser } from './parser';
import { BOT } from './bot';
import { LOGGER } from './logger';
import { INITIATIVE_MODEL } from './models/initiative';
import { v4 } from 'uuid';

export class Handler {
/**
Expand Down Expand Up @@ -67,12 +68,21 @@ export class Handler {
return await BOT.messages.create(message);
}
} catch (e) {
const traceId = v4();
const now = new Date();
LOGGER.error(`TRACE ID: ${traceId}`);
LOGGER.error(`OCCURRED AT: ${now}`);
LOGGER.error(e.stack);
try {
await BOT.messages.create({
markdown: `An unexpected error occurred. Please open an issue by using the "help" command:\n${e}`,
roomId: request.body.data.roomId
});

await BOT.messages.create({
markdown: `An unexpected error occurred at ${now}.\n\`\`\`\nTRACE ID: ${traceId}\n${e.stack}\n\`\`\``,
toPersonEmail: process.env.DEBUG_EMAIL
});
} catch (exc) {
LOGGER.error(exc);
}
Expand All @@ -98,7 +108,7 @@ export class Handler {
}, null, 2)}\n\`\`\``;
LOGGER.debug(debugData);
if (initiative.debug) {
return await BOT.messages.create({ markdown: debugData, ...initiative.destination });
return BOT.messages.create({ markdown: debugData, ...initiative.destination });
}
}
}
2 changes: 1 addition & 1 deletion services/bot/src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export class Parser {
const data = checkData[0];
if (checkData[1] > bestMatch.similarity) {
bestMatch.similarity = checkData[1];
bestMatch.action = (command as any).commandWithArgs;
bestMatch.action = (command as any).commandWithArgs; // eslint-disable-line @typescript-eslint/no-explicit-any
}
// TODO: data type should be easier to work with
if (data instanceof Object && data.action) delete data.action;
Expand Down
26 changes: 25 additions & 1 deletion services/bot/tests/handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import { Handler } from '../src/handler';
import { BOT } from '../src/bot';
import { Request } from 'express';
import { PROJECT_MODEL } from '../src/models/project';
import { CREATE_PROJECT, CREATE_QUEUE } from './util';
import { CREATE_PROJECT, CREATE_QUEUE, STRICT_DATE } from './util';
import MockDate from 'mockdate';

const MOCK_REQUEST = {
'body': {
Expand Down Expand Up @@ -239,6 +240,12 @@ describe('Handler is working', () => {
});

describe('Handler errors as it should', () => {
beforeAll(() => {
MockDate.set(STRICT_DATE);
});
afterAll(() => {
MockDate.reset();
});
test('Handler sends a response even in the event of a catastrophic error', async () => {
BOT.messages.get.mockImplementation(() => {
throw new Error('THIS IS AN EXPECTED ERROR');
Expand All @@ -250,6 +257,23 @@ describe('Handler errors as it should', () => {
roomId: 'mockRequestRoomId',
markdown: 'An unexpected error occurred. Please open an issue by using the "help" command:\nError: THIS IS AN EXPECTED ERROR'
});
expect(BOT.messages.create).toHaveBeenCalledWith({
toPersonEmail: process.env.DEBUG_EMAIL,
markdown: expect.stringMatching(`An unexpected error occurred at Thu May 06 2021 01:43:08 GMT-0400 \\(Eastern Daylight Time\\).
\`\`\`
TRACE ID: [\\w\\-]+
Error: THIS IS AN EXPECTED ERROR
at .*?handler.test.ts:\\d+:\\d+\\)
at .*?index.js:\\d+:\\d+
at .*?index.js:\\d+:\\d+\\)
at .*?index.js:\\d+:\\d+\\)
at .*?parser.ts:\\d+:\\d+\\)
at .*?
at .*?parser.ts:\\d+:\\d+\\)
at .*?
at .*?task_queues:\\d+:\\d+\\)
\`\`\``)
});
});
test('Handler doesnt die when catastrophic error occurs during handling of catastrophic error', async () => {
BOT.messages.get.mockImplementation(() => {
Expand Down
1 change: 1 addition & 0 deletions services/bot/tests/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ process.env.RELEASE_DATE = 'Today';
process.env.AUTHOR_NAME = 'My Author Name';
process.env.AUTHOR_EMAIL = 'email@email.email';
process.env.SUPER_ADMINS = `["${SUPER_ADMIN.id}"]`;
process.env.DEBUG_EMAIL = 'debugemailfoo@debug.email';

beforeAll(async () => {

Expand Down
Loading

0 comments on commit fb05e52

Please sign in to comment.