UNPKG

2.15 kBJavaScriptView Raw
1const notifier = require('node-notifier'),
2 logger = require('./logger');
3
4const shouldExit = error => {
5 const endsWith = str => error.options.uri.endsWith(str);
6
7 if (endsWith('marketplace_releases/sync') || endsWith('/api/graph')) {
8 return false;
9 }
10
11 return true;
12};
13
14const ServerError = {
15 getDetails: errorDetails => {
16 const details = Object.assign({}, errorDetails);
17 try {
18 // most of our errors are with a lot of details from mpbuilder
19 // but some errors are very minimal
20 // TODO: Think about refactoring, maybe whitelist what should be in the error
21 delete details.model_class;
22 delete details.messages;
23 delete details.model_id;
24 delete details.model_hash.body;
25 delete details.model_hash.content;
26 delete details.model_hash.format;
27 delete details.model_hash.partial;
28 } catch (e) {}
29 return details;
30 },
31
32 unauthorized: error => {
33 logger.Debug(`Unauthorized error: ${JSON.stringify(error, null, 2)}`);
34
35 logger.Error(`[${error.statusCode}] ${error.error}`, { hideTimestamp: true });
36 },
37
38 internal: error => {
39 logger.Debug(`Internal error: ${JSON.stringify(error, null, 2)}`);
40 const message = error.error.error;
41 let details = error.error.details;
42
43 // unfortunately we have a lot of different error formats to think about.
44
45 if (typeof details !== 'string') {
46 details = JSON.stringify(ServerError.getDetails(details), null, 2);
47 }
48
49 notifier.notify({ title: 'MarkeplaceKit Error', message: message });
50 logger.Error(`[${error.statusCode}] ${message} \n ${details}`, { hideTimestamp: true, exit: shouldExit(error) });
51 },
52
53 deploy: error => {
54 logger.Debug(`Deploy error: ${JSON.stringify(error, null, 2)}`);
55
56 const message = error.message;
57 let details = error.details;
58
59 if (typeof error.details !== 'string') {
60 details = JSON.stringify(ServerError.getDetails(error.details), null, 2);
61 }
62
63 notifier.notify({ title: 'MarkeplaceKit Error', message: message });
64 logger.Error(`[Deploy] ${message} \n ${details}`, { hideTimestamp: true, exit: false });
65 }
66};
67
68module.exports = ServerError;