Fix order locale and remake Zod def of ErrorApi

This commit is contained in:
CEC 2022-04-21 14:16:18 +02:00
parent 711832bc3a
commit bc17b86ce4
7 changed files with 95 additions and 39 deletions

View File

@ -49,11 +49,11 @@ export class EntryScene extends Scene {
.catch((err) => { .catch((err) => {
const errorType = isErrorApiData.safeParse(err?.response?.data); const errorType = isErrorApiData.safeParse(err?.response?.data);
if (errorType.success) { if (errorType.success) {
// If error from API and urlToRedirect is specified but not buttonTitle => redirect directly if (errorType.data.type === 'unauthorized') {
if (!errorType.data.buttonTitle && errorType.data.urlToRedirect) { void connectionManager.logout();
if (errorType.data.urlToRedirect === "/login") void connectionManager.logout(); } else if (errorType.data.type === 'redirect') {
else window.location.assign(errorType.data.urlToRedirect); window.location.assign(errorType.data.urlToRedirect);
} else errorScreenStore.setError(err.response.data); } else errorScreenStore.setError(err?.response?.data);
} else { } else {
ErrorScene.showError(err, this.scene); ErrorScene.showError(err, this.scene);
} }

View File

@ -9,7 +9,9 @@ function createErrorScreenStore() {
return { return {
subscribe, subscribe,
setError: (e: ErrorScreenMessage): void => set(e), setError: (e: ErrorScreenMessage): void => {
set(e)
},
}; };
} }

View File

@ -5,17 +5,47 @@ import { z } from "zod";
* All other files are automatically copied from this file on container startup / build * All other files are automatically copied from this file on container startup / build
*/ */
export const isErrorApiData = z.object({ export const isErrorApiErrorData = z.object({
type: z.string(), // @ts-ignore
type: z.literal('error'),
code: z.string(), code: z.string(),
title: z.string(), title: z.string(),
subtitle: z.string(), subtitle: z.string(),
details: z.string(), details: z.string(),
image: z.string(), image: z.string(),
urlToRedirect: z.optional(z.nullable(z.string())),
buttonTitle: z.optional(z.nullable(z.string())),
timeToRetry: z.optional(z.nullable(z.bigint())),
canRetryManual: z.optional(z.nullable(z.boolean())),
}); });
export const isErrorApiRetryData = z.object({
type: z.literal('retry'),
code: z.string(),
title: z.string(),
subtitle: z.string(),
details: z.string(),
image: z.string(),
buttonTitle: z.optional(z.nullable(z.string())),
timeToRetry: z.number(),
canRetryManual: z.boolean(),
});
export const isErrorApiRedirectData = z.object({
type: z.literal('redirect'),
urlToRedirect: z.string(),
});
export const isErrorApiUnauthorizedData = z.object({
type: z.literal('unauthorized'),
});
export const isErrorApiData = z.discriminatedUnion("type", [
isErrorApiErrorData,
isErrorApiRetryData,
isErrorApiRedirectData,
isErrorApiUnauthorizedData,
]);
export type ErrorApiErrorData = z.infer<typeof isErrorApiErrorData>;
export type ErrorApiRetryData = z.infer<typeof isErrorApiRetryData>;
export type ErrorApiRedirectData = z.infer<typeof isErrorApiRedirectData>;
export type ErrorApiUnauthorizedData = z.infer<typeof isErrorApiUnauthorizedData>;
export type ErrorApiData = z.infer<typeof isErrorApiData>; export type ErrorApiData = z.infer<typeof isErrorApiData>;

View File

@ -226,15 +226,15 @@ message ErrorMessage {
*/ */
message ErrorScreenMessage { message ErrorScreenMessage {
string type = 1; string type = 1;
string code = 2; google.protobuf.StringValue code = 2;
string title = 3; google.protobuf.StringValue title = 3;
string subtitle = 4; google.protobuf.StringValue subtitle = 4;
string details = 5; google.protobuf.StringValue details = 5;
int32 timeToRetry = 6; google.protobuf.Int32Value timeToRetry = 6;
bool canRetryManual = 7; google.protobuf.BoolValue canRetryManual = 7;
string urlToRedirect = 8; google.protobuf.StringValue urlToRedirect = 8;
string buttonTitle = 9; google.protobuf.StringValue buttonTitle = 9;
string image = 10; google.protobuf.StringValue image = 10;
} }
message ItemStateMessage { message ItemStateMessage {

View File

@ -245,6 +245,8 @@ export class IoSocketController {
const websocketProtocol = req.getHeader("sec-websocket-protocol"); const websocketProtocol = req.getHeader("sec-websocket-protocol");
const websocketExtensions = req.getHeader("sec-websocket-extensions"); const websocketExtensions = req.getHeader("sec-websocket-extensions");
const IPAddress = req.getHeader("x-forwarded-for"); const IPAddress = req.getHeader("x-forwarded-for");
const locale = req.getHeader("accept-language");
const roomId = query.roomId; const roomId = query.roomId;
try { try {
@ -318,7 +320,7 @@ export class IoSocketController {
roomId, roomId,
IPAddress, IPAddress,
characterLayers, characterLayers,
req.getHeader("accept-language") locale
); );
} catch (err) { } catch (err) {
if (Axios.isAxiosError(err)) { if (Axios.isAxiosError(err)) {
@ -336,6 +338,20 @@ export class IoSocketController {
websocketExtensions, websocketExtensions,
context context
); );
} else {
return res.upgrade(
{
rejected: true,
reason: null,
status: 500,
message: err?.response?.data,
roomId: roomId
} as UpgradeFailedData,
websocketKey,
websocketProtocol,
websocketExtensions,
context
);
} }
} }
throw err; throw err;

View File

@ -108,9 +108,9 @@ export class MapController extends BaseHttpController {
try { try {
const mapDetails = isMapDetailsData.parse( const mapDetails = isMapDetailsData.parse(
await adminService.fetchMapDetails( await adminService.fetchMapDetails(
req.header("accept-language"),
query.playUri as string, query.playUri as string,
query.authToken as string query.authToken as string,
req.header("accept-language")
) )
); );

View File

@ -55,7 +55,13 @@ import { ExAdminSocketInterface } from "../Model/Websocket/ExAdminSocketInterfac
import { compressors } from "hyper-express"; import { compressors } from "hyper-express";
import { isMapDetailsData } from "../Messages/JsonMessages/MapDetailsData"; import { isMapDetailsData } from "../Messages/JsonMessages/MapDetailsData";
import { adminService } from "./AdminService"; import { adminService } from "./AdminService";
import { ErrorApiData } from "../Messages/JsonMessages/ErrorApiData"; import {
ErrorApiData,
isErrorApiErrorData,
isErrorApiRedirectData,
isErrorApiRetryData, isErrorApiUnauthorizedData
} from "../Messages/JsonMessages/ErrorApiData";
import {BoolValue, Int32Value, StringValue} from "google-protobuf/google/protobuf/wrappers_pb";
const debug = Debug("socket"); const debug = Debug("socket");
@ -472,7 +478,7 @@ export class SocketManager implements ZoneEventListener {
} }
public async updateRoomWithAdminData(room: PusherRoom): Promise<void> { public async updateRoomWithAdminData(room: PusherRoom): Promise<void> {
const data = await adminService.fetchMapDetails("en", room.roomUrl); const data = await adminService.fetchMapDetails(room.roomUrl);
const mapDetailsData = isMapDetailsData.safeParse(data); const mapDetailsData = isMapDetailsData.safeParse(data);
if (mapDetailsData.success) { if (mapDetailsData.success) {
@ -672,21 +678,23 @@ export class SocketManager implements ZoneEventListener {
client.send(serverToClientMessage.serializeBinary().buffer, true); client.send(serverToClientMessage.serializeBinary().buffer, true);
} }
public emitErrorScreenMessage(client: compressors.WebSocket, error: ErrorApiData) { public emitErrorScreenMessage(client: compressors.WebSocket, errorApi: ErrorApiData) {
const errorMessage = new ErrorScreenMessage(); const errorMessage = new ErrorScreenMessage();
errorMessage.setType(error.type); errorMessage.setType(errorApi.type);
errorMessage.setCode(error.code); if(errorApi.type == 'retry' || errorApi.type == 'error'){
errorMessage.setTitle(error.title); errorMessage.setCode(new StringValue().setValue(errorApi.code));
errorMessage.setSubtitle(error.subtitle); errorMessage.setTitle(new StringValue().setValue(errorApi.title));
errorMessage.setDetails(error.details); errorMessage.setSubtitle(new StringValue().setValue(errorApi.subtitle));
errorMessage.setImage(error.image); errorMessage.setDetails(new StringValue().setValue(errorApi.details));
errorMessage.setImage(new StringValue().setValue(errorApi.image));
}
if(errorApi.type == 'retry') {
if (errorApi.buttonTitle) errorMessage.setButtontitle(new StringValue().setValue(errorApi.buttonTitle));
if (errorApi.canRetryManual !== undefined) errorMessage.setCanretrymanual(new BoolValue().setValue(errorApi.canRetryManual));
if (errorApi.timeToRetry) errorMessage.setTimetoretry(new Int32Value().setValue(Number(errorApi.timeToRetry)));
}
if(errorApi.type == 'redirect' && errorApi.urlToRedirect) errorMessage.setUrltoredirect(new StringValue().setValue(errorApi.urlToRedirect));
if (error.urlToRedirect) errorMessage.setUrltoredirect(error.urlToRedirect);
if (error.buttonTitle) errorMessage.setButtontitle(error.buttonTitle);
if (error.canRetryManual !== undefined && null !== error.canRetryManual)
errorMessage.setCanretrymanual(error.canRetryManual);
if (error.timeToRetry && !isNaN(Number(error.timeToRetry)))
errorMessage.setTimetoretry(Number(error.timeToRetry));
const serverToClientMessage = new ServerToClientMessage(); const serverToClientMessage = new ServerToClientMessage();
serverToClientMessage.setErrorscreenmessage(errorMessage); serverToClientMessage.setErrorscreenmessage(errorMessage);