Skip to content

Commit

Permalink
Error types (hathora#123)
Browse files Browse the repository at this point in the history
* Error types

* update failures

* update

* update
  • Loading branch information
varunm authored Jan 7, 2022
1 parent 93c2aeb commit 3be8ba9
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 8 deletions.
7 changes: 4 additions & 3 deletions templates/base/client/.rtag/App.tsx.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ import { getUserDisplayName, UserData } from "./base";
import { RtagContext } from "./context";
import { Forms, InitializeForm } from "./Forms";
import { State } from "./State";
import { ConnectionFailure } from "./failures";

const client = new RtagClient(import.meta.env.VITE_APP_ID);

function App() {
const [token, setToken] = useState<string | undefined>(sessionStorage.getItem("token") ?? undefined);
const [connection, setConnection] = useState<RtagConnection>();
const [updateArgs, setUpdateArgs] = useState<UpdateArgs>();
const [connectionError, setConnectionError] = useState<string>();
const [connectionError, setConnectionError] = useState<ConnectionFailure>();
const location = useLocation();
const navigate = useNavigate();

Expand Down Expand Up @@ -198,7 +199,7 @@ type MainProps = {
user: UserData;
connection: RtagConnection | undefined;
updateArgs: UpdateArgs | undefined;
connectionError: string | undefined;
connectionError: ConnectionFailure | undefined;
onConnect: (stateId: string) => void;
onDisconnect: () => void;
};
Expand All @@ -213,7 +214,7 @@ function Main({ user, connection, connectionError, updateArgs, onConnect, onDisc
}, [connection]);

if (connectionError !== undefined) {
return <div>Connection error: {connectionError}</div>;
return <div>Connection error: {connectionError.message}</div>;
}

if (connection === undefined || updateArgs === undefined) {
Expand Down
10 changes: 5 additions & 5 deletions templates/base/client/.rtag/client.ts.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import getRandomValues from "get-random-values";
import axios from "axios";
import jwtDecode from "jwt-decode";
import { UserData, Response, Method } from "./base";
import { ConnectionFailure, transformCoordinatorFailure } from "./failures";
import { computePatch } from "./patch";
import {
decodeStateUpdate,
Expand Down Expand Up @@ -42,7 +43,7 @@ export class RtagClient {
token: string,
request: {{makeRequestName initialize}},
onUpdate: (updateArgs: UpdateArgs) => void,
onConnectionFailure: (error: string) => void
onConnectionFailure: (failure: ConnectionFailure) => void
): Promise<RtagConnection> {
return new Promise((resolve) => {
const socket = this.initSocket(onConnectionFailure);
Expand All @@ -58,7 +59,7 @@ export class RtagClient {
token: string,
stateId: StateId,
onUpdate: (updateArgs: UpdateArgs) => void,
onConnectionFailure: (error: string) => void
onConnectionFailure: (failure: ConnectionFailure) => void
): RtagConnection {
const socket = this.initSocket(onConnectionFailure);
socket.onopen = () => {
Expand All @@ -68,11 +69,10 @@ export class RtagClient {
return new RtagConnection(stateId, socket, onUpdate);
}

private initSocket(onConnectionFailure: (error: string) => void) {
private initSocket(onConnectionFailure: (failure: ConnectionFailure) => void) {
const socket = new WebSocket(`wss://${COORDINATOR_HOST}/${this.appId}`);
socket.binaryType = "arraybuffer";
socket.onerror = (e) => onConnectionFailure(e.message);
socket.onclose = (e) => onConnectionFailure(e.reason);
socket.onclose = (e) => onConnectionFailure(transformCoordinatorFailure(e));
return socket;
}
}
Expand Down
32 changes: 32 additions & 0 deletions templates/base/client/.rtag/failures.ts.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
export enum ConnectionFailureType {
STATE_NOT_FOUND = "STATE_NOT_FOUND",
NO_AVAILABLE_STORES = "NO_AVAILABLE_STORES",
INVALID_USER_DATA = "INVALID_USER_DATA",
INVALID_STATE_ID = "INVALID_STATE_ID",
GENERIC_FAILURE = "GENERIC_FAILURE",
}

export interface ConnectionFailure {
type: ConnectionFailureType,
message: string;
}

export const transformCoordinatorFailure = (e: {code: number, reason: string}): ConnectionFailure => {
return {
message: e.reason,
type: (function(code) {
switch (code) {
case 4000:
return ConnectionFailureType.STATE_NOT_FOUND;
case 4001:
return ConnectionFailureType.NO_AVAILABLE_STORES;
case 4002:
return ConnectionFailureType.INVALID_USER_DATA;
case 4003:
return ConnectionFailureType.INVALID_STATE_ID;
default:
return ConnectionFailureType.GENERIC_FAILURE;
}
})(e.code)
};
}

0 comments on commit 3be8ba9

Please sign in to comment.