Skip to content

Commit

Permalink
fix(protocol): put protocol-error code in server_internal_server data
Browse files Browse the repository at this point in the history
  • Loading branch information
fenying committed Jun 30, 2024
1 parent 0d150f5 commit 59d1e1a
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 17 deletions.
3 changes: 2 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
51 changes: 38 additions & 13 deletions src/examples/shared/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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': {
Expand All @@ -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 {

Expand All @@ -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 {

Expand Down
1 change: 1 addition & 0 deletions src/examples/shared/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/shared/Encodings/v2/DecoderV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ class TvErrorResponseDecoder implements IPacketDecoder {
}
else {

err = new ProtocolError(errMsg, null);
err = new ProtocolError(errMsg, null, null);
}
}
else {
Expand Down
5 changes: 3 additions & 2 deletions src/lib/shared/Errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export class ProtocolError extends TelevokeError {

public constructor(
message: string,
public readonly data: unknown,
origin: unknown,
) {

Expand All @@ -52,9 +53,9 @@ function defineProtocolError<T extends string>(name: T): Record<T, new (

[name]: class extends ProtocolError {

public constructor(public readonly data: unknown = null, origin: unknown = null) {
public constructor(data: unknown = null, origin: unknown = null) {

super(name, origin);
super(name, data, origin);
}
},
} as any;
Expand Down
13 changes: 13 additions & 0 deletions src/lib/transporters/legacy-http/LegacyHttp.Server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,19 @@ class LegacyHttpGateway extends EventEmitter implements dT.IGateway {
recvAt
));
}
else if (result instanceof Shared.ProtocolError) {

this._sendResponse(resp, encoder.encodeApiErrorResponse(
input.rid,
v1.EResponseCode.SYSTEM_ERROR,
JSON.stringify({
name: result.name,
message: result.message,
data: result.data,
}),
recvAt
));
}
else {

this._sendResponse(resp, encoder.encodeApiErrorResponse(
Expand Down

0 comments on commit 59d1e1a

Please sign in to comment.