Skip to content

Commit

Permalink
feat(api): properly forward API errors to the terminal
Browse files Browse the repository at this point in the history
  • Loading branch information
tomastrajan committed Mar 22, 2023
1 parent 529f2bd commit 912a9ca
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions lib/services/api.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import got, { Got } from 'got';

import { createLogger } from './logger.service';
import * as process from 'process';

let api: Got;
const logger = createLogger('API SERVICE');
Expand All @@ -24,10 +25,24 @@ export const createApiService = (argv: any) => {
? 'http://localhost:8080'
: apiUrl ?? 'https://api.omniboard.dev',
headers: {
'Content-Type': 'application/json',
'omniboard-api-key': key,
},
hooks: {
beforeRequest: debug ? [(options) => logger.info(options)] : [],
beforeError: [
error => {
const {response} = error;
if (response) {
let body: any = response.body;
if (body) {
error.name = `${body.error} ${body.statusCode}`;
error.message = `${body.message}`;
}
}
return error;
}
]
},
});
};
Expand Down

0 comments on commit 912a9ca

Please sign in to comment.