From 4234b3891083c7f742ecebc27154e91aea612a5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20N=C3=A9grier?= Date: Sun, 17 Jan 2021 22:40:54 +0100 Subject: [PATCH] Better error message in case of 404 --- front/src/Phaser/Login/EntryScene.ts | 8 ++++-- front/src/Phaser/Reconnecting/ErrorScene.ts | 27 ++++++++++++++++++--- front/src/Phaser/Reconnecting/WAError.ts | 26 ++++++++++++++++++++ pusher/src/Services/SocketManager.ts | 3 ++- 4 files changed, 58 insertions(+), 6 deletions(-) create mode 100644 front/src/Phaser/Reconnecting/WAError.ts diff --git a/front/src/Phaser/Login/EntryScene.ts b/front/src/Phaser/Login/EntryScene.ts index fc551375..75ee0272 100644 --- a/front/src/Phaser/Login/EntryScene.ts +++ b/front/src/Phaser/Login/EntryScene.ts @@ -1,7 +1,7 @@ import {gameManager} from "../Game/GameManager"; import {Scene} from "phaser"; -import {handleAxiosError} from "../../Network/axios"; import {ErrorScene} from "../Reconnecting/ErrorScene"; +import {WAError} from "../Reconnecting/WAError"; export const EntrySceneName = "EntryScene"; @@ -20,7 +20,11 @@ export class EntryScene extends Scene { gameManager.init(this.scene).then((nextSceneName) => { this.scene.start(nextSceneName); }).catch((err) => { - ErrorScene.showError(err, this.scene); + if (err.response && err.response.status == 404) { + ErrorScene.showError(new WAError('Page Not Found', 'Could not find map', window.location.pathname), this.scene); + } else { + ErrorScene.showError(err, this.scene); + } }); } } diff --git a/front/src/Phaser/Reconnecting/ErrorScene.ts b/front/src/Phaser/Reconnecting/ErrorScene.ts index 8253c5e8..d7fc2ab7 100644 --- a/front/src/Phaser/Reconnecting/ErrorScene.ts +++ b/front/src/Phaser/Reconnecting/ErrorScene.ts @@ -3,6 +3,7 @@ import Image = Phaser.GameObjects.Image; import Sprite = Phaser.GameObjects.Sprite; import Text = Phaser.GameObjects.Text; import ScenePlugin = Phaser.Scenes.ScenePlugin; +import {WAError} from "./WAError"; export const ErrorSceneName = "ErrorScene"; enum Textures { @@ -26,7 +27,7 @@ export class ErrorScene extends Phaser.Scene { }); } - init({ title, subTitle, message }: { title?: string, subTitle?: string, message?: string }) { + init({title, subTitle, message}: { title?: string, subTitle?: string, message?: string }) { this.title = title ? title : ''; this.subTitle = subTitle ? subTitle : ''; this.message = message ? message : ''; @@ -51,11 +52,14 @@ export class ErrorScene extends Phaser.Scene { this.subTitleField = new TextField(this, this.game.renderer.width / 2, this.game.renderer.height / 2 + 24, this.subTitle); - this.messageField = this.add.text(this.game.renderer.width / 2, this.game.renderer.height / 2 + 38, this.message, { fontFamily: 'Georgia, "Goudy Bookletter 1911", Times, serif', fontSize: '10px' }); + this.messageField = this.add.text(this.game.renderer.width / 2, this.game.renderer.height / 2 + 38, this.message, { + fontFamily: 'Georgia, "Goudy Bookletter 1911", Times, serif', + fontSize: '10px' + }); this.messageField.setOrigin(0.5, 0.5); this.cat = this.physics.add.sprite(this.game.renderer.width / 2, this.game.renderer.height / 2 - 32, 'cat', 6); - this.cat.flipY=true; + this.cat.flipY = true; } /** @@ -69,6 +73,12 @@ export class ErrorScene extends Phaser.Scene { title: 'An error occurred', subTitle: error }); + } else if (error instanceof WAError) { + scene.start(ErrorSceneName, { + title: error.title, + subTitle: error.subTitle, + message: error.details + }); } else if (error.response) { // Axios HTTP error // client received an error response (5xx, 4xx) @@ -95,4 +105,15 @@ export class ErrorScene extends Phaser.Scene { throw error; } } + + /** + * Displays the error page, with an error message matching the "error" parameters passed in. + */ + public static startErrorPage(title: string, subTitle: string, message: string, scene: ScenePlugin): void { + scene.start(ErrorSceneName, { + title, + subTitle, + message + }); + } } diff --git a/front/src/Phaser/Reconnecting/WAError.ts b/front/src/Phaser/Reconnecting/WAError.ts new file mode 100644 index 00000000..cdc433b6 --- /dev/null +++ b/front/src/Phaser/Reconnecting/WAError.ts @@ -0,0 +1,26 @@ +export class WAError extends Error { + private _title: string; + private _subTitle: string; + private _details: string; + + constructor (title: string, subTitle: string, details: string) { + super(title+' - '+subTitle+' - '+details); + this._title = title; + this._subTitle = subTitle; + this._details = details; + // Set the prototype explicitly. + Object.setPrototypeOf (this, WAError.prototype); + } + + get title(): string { + return this._title; + } + + get subTitle(): string { + return this._subTitle; + } + + get details(): string { + return this._details; + } +} diff --git a/pusher/src/Services/SocketManager.ts b/pusher/src/Services/SocketManager.ts index ff5d25cf..f5915ddb 100644 --- a/pusher/src/Services/SocketManager.ts +++ b/pusher/src/Services/SocketManager.ts @@ -22,7 +22,8 @@ import { PusherToBackMessage, AdminPusherToBackMessage, ServerToAdminClientMessage, - AdminMessage, SendUserMessage, BanUserMessage + SendUserMessage, + BanUserMessage } from "../Messages/generated/messages_pb"; import {PointInterface} from "../Model/Websocket/PointInterface"; import {ProtobufUtils} from "../Model/Websocket/ProtobufUtils";