partey_workadventure/back/src/Services/MessageHelpers.ts

75 lines
2.1 KiB
TypeScript
Raw Normal View History

import {
BatchMessage,
BatchToPusherMessage,
BatchToPusherRoomMessage,
ErrorMessage,
ServerToClientMessage,
SubToPusherMessage,
SubToPusherRoomMessage,
} from "../Messages/generated/messages_pb";
2021-06-24 10:09:10 +02:00
import { UserSocket } from "_Model/User";
import { RoomSocket, ZoneSocket } from "../RoomManager";
2020-12-04 15:58:31 +01:00
2021-12-23 15:27:17 +01:00
function getMessageFromError(error: unknown): string {
if (error instanceof Error) {
2021-12-23 15:27:17 +01:00
return error.message;
} else if (typeof error === "string") {
2021-12-23 15:27:17 +01:00
return error;
} else {
2021-12-23 15:27:17 +01:00
return "Unknown error";
}
2021-12-23 15:27:17 +01:00
}
export function emitError(Client: UserSocket, error: unknown): void {
const message = getMessageFromError(error);
2020-12-04 15:58:31 +01:00
const errorMessage = new ErrorMessage();
errorMessage.setMessage(message);
const serverToClientMessage = new ServerToClientMessage();
serverToClientMessage.setErrormessage(errorMessage);
//if (!Client.disconnecting) {
2021-06-24 10:09:10 +02:00
Client.write(serverToClientMessage);
2020-12-04 15:58:31 +01:00
//}
2021-11-24 16:20:07 +01:00
console.warn(message);
2020-12-04 15:58:31 +01:00
}
2021-12-23 15:27:17 +01:00
export function emitErrorOnRoomSocket(Client: RoomSocket, error: unknown): void {
console.error(error);
const message = getMessageFromError(error);
const errorMessage = new ErrorMessage();
errorMessage.setMessage(message);
const subToPusherRoomMessage = new SubToPusherRoomMessage();
subToPusherRoomMessage.setErrormessage(errorMessage);
const batchToPusherMessage = new BatchToPusherRoomMessage();
batchToPusherMessage.addPayload(subToPusherRoomMessage);
//if (!Client.disconnecting) {
Client.write(batchToPusherMessage);
//}
2021-11-24 16:20:07 +01:00
console.warn(message);
}
2021-12-23 15:27:17 +01:00
export function emitErrorOnZoneSocket(Client: ZoneSocket, error: unknown): void {
console.error(error);
const message = getMessageFromError(error);
const errorMessage = new ErrorMessage();
errorMessage.setMessage(message);
const subToPusherMessage = new SubToPusherMessage();
subToPusherMessage.setErrormessage(errorMessage);
const batchToPusherMessage = new BatchToPusherMessage();
batchToPusherMessage.addPayload(subToPusherMessage);
//if (!Client.disconnecting) {
Client.write(batchToPusherMessage);
//}
2021-11-24 16:20:07 +01:00
console.warn(message);
}