Skip to content

Commit

Permalink
chore(server): add on reject error handler
Browse files Browse the repository at this point in the history
  • Loading branch information
Kharya1337 committed Jan 16, 2024
1 parent a8d4926 commit 53cddc5
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions packages/server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export interface IServerOptions extends ServerOptions {
/**
* Prepares server for message handling (auth, validation, setup, etc.).
*/
onMessage?: (session: Session, action: proto.ActionProto) => Promise<unknown>;
onMessage?: (session: Session, action: proto.ActionProto) => Promise<(error: Error) => unknown>;
}

/**
Expand Down Expand Up @@ -132,14 +132,24 @@ export class LocalServer extends core.EventLogEmitter {
})
.on("message", (e) => {
(async () => {
let onReject = (reason: Error) => {
e.reject(reason);
};

if (this.onMessageHandler) {
await this.onMessageHandler(e.session, e.message);
const onError = await this.onMessageHandler(e.session, e.message);

onReject = (reason) => {
onError(reason);

e.reject(reason);
};
}

if (e.message.action === proto.ServerIsLoggedInActionProto.ACTION ||
e.message.action === proto.ServerLoginActionProto.ACTION) {
this.onMessage(e.session, e.message)
.then(e.resolve, e.reject);
.then(e.resolve, onReject)
}
})()
.catch((error) => {
Expand Down

0 comments on commit 53cddc5

Please sign in to comment.