From 59d1e1a5e57bb7ce7eb21ebdca5e9a330cf10088 Mon Sep 17 00:00:00 2001 From: Angus ZENG Date: Sun, 30 Jun 2024 19:54:38 +0800 Subject: [PATCH] fix(protocol): put protocol-error code in server_internal_server data --- CHANGES.md | 3 +- src/examples/shared/client.ts | 51 ++++++++++++++----- src/examples/shared/router.ts | 1 + src/lib/shared/Encodings/v2/DecoderV2.ts | 2 +- src/lib/shared/Errors.ts | 5 +- .../legacy-http/LegacyHttp.Server.ts | 13 +++++ 6 files changed, 58 insertions(+), 17 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 26ed092..800bff3 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -3,8 +3,9 @@ ## v1.1.0 - feat(protocol): added experimental supports for transports between worker thread. -- fix(protocol): Simplified the ITransporter.end method. +- fix(protocol): simplified the ITransporter.end method. - fix(decoder): should decode protocol error as special error. +- fix(protocol): put protocol-error code in server_internal_server data. ## v1.0.3 diff --git a/src/examples/shared/client.ts b/src/examples/shared/client.ts index 9020bc4..943cbb9 100644 --- a/src/examples/shared/client.ts +++ b/src/examples/shared/client.ts @@ -209,13 +209,21 @@ export async function doClientTest( } catch (e) { - if (connLess && e instanceof Tv.errors.server_internal_error) { + if (connLess && e instanceof Tv.errors.server_internal_error && (e.data as any)?.body?.message) { - clientLogs.warning(mkMsg(`Specific command is not implemented by current protocol, skipped`)); - break; + switch ((e.data as any)?.body?.message) { + + case 'cmd_not_impl': + clientLogs.warning(mkMsg(`Specific command is not implemented by current protocol, skipped`)); + break; + default: + clientLogs.error(mkMsg(`Unexpected error thrown, error: ${e}`)); + } } + else { - clientLogs.error(mkMsg(`Unexpected error thrown, error: ${e}`)); + clientLogs.error(mkMsg(`Unexpected error thrown, error: ${e}`)); + } } break; case 'send_stream': { @@ -229,15 +237,19 @@ export async function doClientTest( } catch (e) { - if (connLess && e instanceof Tv.errors.server_internal_error) { + if (connLess && e instanceof Tv.errors.server_internal_error && (e.data as any)?.body?.message) { - clientLogs.warning(mkMsg(`Specific command is not implemented by current protocol, skipped`)); - break; - } - - if (e instanceof Tv.errors.system_busy) { + switch ((e.data as any)?.body?.message) { - clientLogs.warning(mkMsg(`Server is busy, skipped`)); + case 'cmd_not_impl': + clientLogs.warning(mkMsg(`Specific command is not implemented by current protocol, skipped: ${e}`)); + break; + case 'system_busy': + clientLogs.warning(mkMsg(`Server is busy, skipped: ${e}`)); + break; + default: + clientLogs.error(mkMsg(`Unexpected error thrown, error: ${e}`)); + } } else { @@ -259,10 +271,23 @@ export async function doClientTest( } catch (e) { - if (connLess && e instanceof Tv.errors.cmd_not_impl) { + if (connLess && e instanceof Tv.errors.server_internal_error && (e.data as any)?.body?.message) { + + switch ((e.data as any)?.body?.message) { + + case 'cmd_not_impl': + clientLogs.warning(mkMsg(`Specific command is not implemented by current protocol, skipped`)); + break; + case 'system_busy': + clientLogs.warning(mkMsg(`Server is busy, skipped: ${e}`)); + break; + default: + clientLogs.error(mkMsg(`Unexpected error thrown, error: ${e}`)); + } + } + else if (e instanceof Tv.errors.cmd_not_impl) { clientLogs.warning(mkMsg(`Specific command is not implemented by current protocol, skipped`)); - break; } else { diff --git a/src/examples/shared/router.ts b/src/examples/shared/router.ts index 65eef13..db3926b 100644 --- a/src/examples/shared/router.ts +++ b/src/examples/shared/router.ts @@ -62,6 +62,7 @@ export const router: Tv.Servers.IRouter = new Tv.Servers.SimpleJsonApiRouter() } catch (e) { serverLogs.error(`Failed while sending message, error: ${e}`); + throw e; } }) .registerApi('replyNonValidDataInJson', (ctx): unknown => { diff --git a/src/lib/shared/Encodings/v2/DecoderV2.ts b/src/lib/shared/Encodings/v2/DecoderV2.ts index 5ebb6bc..302d7dc 100644 --- a/src/lib/shared/Encodings/v2/DecoderV2.ts +++ b/src/lib/shared/Encodings/v2/DecoderV2.ts @@ -196,7 +196,7 @@ class TvErrorResponseDecoder implements IPacketDecoder { } else { - err = new ProtocolError(errMsg, null); + err = new ProtocolError(errMsg, null, null); } } else { diff --git a/src/lib/shared/Errors.ts b/src/lib/shared/Errors.ts index 52883c9..632dbeb 100644 --- a/src/lib/shared/Errors.ts +++ b/src/lib/shared/Errors.ts @@ -36,6 +36,7 @@ export class ProtocolError extends TelevokeError { public constructor( message: string, + public readonly data: unknown, origin: unknown, ) { @@ -52,9 +53,9 @@ function defineProtocolError(name: T): Record