Fix error handling to display them correctly without crashing
This commit is contained in:
@@ -4,6 +4,6 @@ import { AppModule } from './app.module';
|
||||
async function bootstrap() {
|
||||
const app = await NestFactory.create(AppModule);
|
||||
app.enableCors();
|
||||
await app.listen(3000);
|
||||
await app.listen(3500);
|
||||
}
|
||||
bootstrap();
|
||||
|
||||
12
backend/src/models/RequestError.ts
Normal file
12
backend/src/models/RequestError.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
// @ts-ignore
|
||||
interface RequestError extends Error {
|
||||
response?: any;
|
||||
}
|
||||
|
||||
interface RequestErrorConstructor extends ErrorConstructor {
|
||||
new(message?: string): RequestError;
|
||||
(message?: string): RequestError;
|
||||
readonly prototype: RequestError;
|
||||
}
|
||||
|
||||
declare var RequestError: RequestErrorConstructor;
|
||||
@@ -31,7 +31,7 @@ export class SinusBotService {
|
||||
.then(token => this.bearer = token)
|
||||
.catch(error => {
|
||||
logger.error(`Oh oh! Something went wrong while fetching Bearer token.`, error);
|
||||
throw this.createHttpException(HttpStatus.UNAUTHORIZED, error.message);
|
||||
throw this.createHttpException(HttpStatus.UNAUTHORIZED, `Fetching Bearer token for Sinusbot failed.`);
|
||||
});
|
||||
logger.debug(
|
||||
`Seems like I have my Bearer token!
|
||||
@@ -46,7 +46,7 @@ export class SinusBotService {
|
||||
.then(data => this.consumeTunakillResponse(data))
|
||||
.catch(error => {
|
||||
logger.error(`I couldn't fetch user data.`, error);
|
||||
throw this.createHttpException(error.statusCode, error.message);
|
||||
throw this.createHttpException(HttpStatus.INTERNAL_SERVER_ERROR, error.message);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -90,12 +90,12 @@ export class SinusBotService {
|
||||
|
||||
private consumeTunakillResponse(data: any): TableEntry[] {
|
||||
if (!(data !== null || data[0] !== null || data[0].data !== null)) {
|
||||
throw Error('Response from SinusBot does not have any data to parse.')
|
||||
throw Error('Response from SinusBot does not have any data to parse.');
|
||||
}
|
||||
|
||||
const response = data[0].data;
|
||||
if (!(response.length > 0)) {
|
||||
throw Error('User list is empty.')
|
||||
throw Error('Response from SinusBot does not have any data to parse.');
|
||||
}
|
||||
|
||||
return response
|
||||
@@ -134,7 +134,9 @@ export class SinusBotService {
|
||||
|
||||
private checkStatus = response => {
|
||||
if (!response.ok) {
|
||||
throw Error(response.statusText);
|
||||
let err = new RequestError(response.errorText);
|
||||
err.response = response;
|
||||
throw err;
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user