From cd7a332b4c011ce6a8cbc7c681a9d1ac6f365c1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20N=C3=A9grier?= Date: Wed, 31 Mar 2021 15:48:25 +0200 Subject: [PATCH] Improving error throwing and handling in pusher/admin/front --- back/src/Services/AdminApi.ts | 2 +- front/src/Connexion/ConnectionManager.ts | 2 +- pusher/src/Controller/BaseController.ts | 16 ++++++++++++++-- pusher/src/Services/AdminApi.ts | 10 +++++----- 4 files changed, 21 insertions(+), 9 deletions(-) diff --git a/back/src/Services/AdminApi.ts b/back/src/Services/AdminApi.ts index ef969a76..0c53e3b3 100644 --- a/back/src/Services/AdminApi.ts +++ b/back/src/Services/AdminApi.ts @@ -24,7 +24,7 @@ class AdminApi { async fetchMapDetails(organizationSlug: string, worldSlug: string, roomSlug: string|undefined): Promise { if (!ADMIN_API_URL) { - return Promise.reject('No admin backoffice set!'); + return Promise.reject(new Error('No admin backoffice set!')); } const params: { organizationSlug: string, worldSlug: string, roomSlug?: string } = { diff --git a/front/src/Connexion/ConnectionManager.ts b/front/src/Connexion/ConnectionManager.ts index ed920aa0..aa679b5e 100644 --- a/front/src/Connexion/ConnectionManager.ts +++ b/front/src/Connexion/ConnectionManager.ts @@ -69,7 +69,7 @@ class ConnectionManager { return Promise.resolve(new Room(roomId)); } - return Promise.reject('Invalid URL'); + return Promise.reject(new Error('Invalid URL')); } private async verifyToken(token: string): Promise { diff --git a/pusher/src/Controller/BaseController.ts b/pusher/src/Controller/BaseController.ts index bb500b57..91882138 100644 --- a/pusher/src/Controller/BaseController.ts +++ b/pusher/src/Controller/BaseController.ts @@ -13,8 +13,20 @@ export class BaseController { */ // eslint-disable-next-line @typescript-eslint/no-explicit-any protected errorToResponse(e: any, res: HttpResponse): void { - console.error(e.message || "An error happened.", e?.config.url); - console.error(e.stack || 'no stack defined.'); + if (e && e.message) { + let url = e?.config?.url; + if (url !== undefined) { + url = ' for URL: '+url; + } else { + url = ''; + } + console.error('ERROR: '+e.message+url); + } else if (typeof(e) === 'string') { + console.error(e); + } + if (e.stack) { + console.error(e.stack); + } if (e.response) { res.writeStatus(e.response.status+" "+e.response.statusText); this.addCorsHeaders(res); diff --git a/pusher/src/Services/AdminApi.ts b/pusher/src/Services/AdminApi.ts index d50e2a4f..0cc6d43e 100644 --- a/pusher/src/Services/AdminApi.ts +++ b/pusher/src/Services/AdminApi.ts @@ -37,7 +37,7 @@ class AdminApi { async fetchMapDetails(organizationSlug: string, worldSlug: string, roomSlug: string|undefined): Promise { if (!ADMIN_API_URL) { - return Promise.reject('No admin backoffice set!'); + return Promise.reject(new Error('No admin backoffice set!')); } const params: { organizationSlug: string, worldSlug: string, roomSlug?: string } = { @@ -60,7 +60,7 @@ class AdminApi { async fetchMemberDataByUuid(uuid: string, roomId: string): Promise { if (!ADMIN_API_URL) { - return Promise.reject('No admin backoffice set!'); + return Promise.reject(new Error('No admin backoffice set!')); } const res = await Axios.get(ADMIN_API_URL+'/api/room/access', { params: {uuid, roomId}, headers: {"Authorization" : `${ADMIN_API_TOKEN}`} } @@ -70,7 +70,7 @@ class AdminApi { async fetchMemberDataByToken(organizationMemberToken: string): Promise { if (!ADMIN_API_URL) { - return Promise.reject('No admin backoffice set!'); + return Promise.reject(new Error('No admin backoffice set!')); } //todo: this call can fail if the corresponding world is not activated or if the token is invalid. Handle that case. const res = await Axios.get(ADMIN_API_URL+'/api/login-url/'+organizationMemberToken, @@ -81,7 +81,7 @@ class AdminApi { async fetchCheckUserByToken(organizationMemberToken: string): Promise { if (!ADMIN_API_URL) { - return Promise.reject('No admin backoffice set!'); + return Promise.reject(new Error('No admin backoffice set!')); } //todo: this call can fail if the corresponding world is not activated or if the token is invalid. Handle that case. const res = await Axios.get(ADMIN_API_URL+'/api/check-user/'+organizationMemberToken, @@ -104,7 +104,7 @@ class AdminApi { async verifyBanUser(organizationMemberToken: string, ipAddress: string, organization: string, world: string): Promise { if (!ADMIN_API_URL) { - return Promise.reject('No admin backoffice set!'); + return Promise.reject(new Error('No admin backoffice set!')); } //todo: this call can fail if the corresponding world is not activated or if the token is invalid. Handle that case. return Axios.get(ADMIN_API_URL + '/api/check-moderate-user/'+organization+'/'+world+'?ipAddress='+ipAddress+'&token='+organizationMemberToken,