Skip to content

Commit 4e23a1f

Browse files
committed
Added support for custom RSocketError
1 parent cdfa824 commit 4e23a1f

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

packages/rsocket-core/src/RSocketMachine.js

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ class RSocketMachineImpl<D, M> implements RSocketMachine<D, M> {
443443
this._sendStreamComplete(streamId);
444444
},
445445
onError: error => {
446-
this._sendStreamError(streamId, error.message);
446+
this._sendStreamError(streamId, error);
447447
},
448448
//Subscriber methods
449449
onNext: payload => {
@@ -677,7 +677,7 @@ class RSocketMachineImpl<D, M> implements RSocketMachine<D, M> {
677677
if (this._isRequest(frame.type)) {
678678
const leaseError = this._useLeaseOrError(this._responderLeaseHandler);
679679
if (leaseError) {
680-
this._sendStreamError(streamId, leaseError);
680+
this._sendStreamError(streamId, new Error(leaseError));
681681
return;
682682
}
683683
}
@@ -758,7 +758,7 @@ class RSocketMachineImpl<D, M> implements RSocketMachine<D, M> {
758758
onComplete: payload => {
759759
this._sendStreamPayload(streamId, payload, true);
760760
},
761-
onError: error => this._sendStreamError(streamId, error.message),
761+
onError: error => this._sendStreamError(streamId, error),
762762
onSubscribe: cancel => {
763763
const subscription = {
764764
cancel,
@@ -773,7 +773,7 @@ class RSocketMachineImpl<D, M> implements RSocketMachine<D, M> {
773773
const payload = this._deserializePayload(frame);
774774
this._requestHandler.requestStream(payload).subscribe({
775775
onComplete: () => this._sendStreamComplete(streamId),
776-
onError: error => this._sendStreamError(streamId, error.message),
776+
onError: error => this._sendStreamError(streamId, error),
777777
onNext: payload => this._sendStreamPayload(streamId, payload),
778778
onSubscribe: subscription => {
779779
this._subscriptions.set(streamId, subscription);
@@ -835,7 +835,7 @@ class RSocketMachineImpl<D, M> implements RSocketMachine<D, M> {
835835

836836
this._requestHandler.requestChannel(framesToPayloads).subscribe({
837837
onComplete: () => this._sendStreamComplete(streamId),
838-
onError: error => this._sendStreamError(streamId, error.message),
838+
onError: error => this._sendStreamError(streamId, error),
839839
onNext: payload => this._sendStreamPayload(streamId, payload),
840840
onSubscribe: subscription => {
841841
this._subscriptions.set(streamId, subscription);
@@ -864,16 +864,16 @@ class RSocketMachineImpl<D, M> implements RSocketMachine<D, M> {
864864
});
865865
}
866866

867-
_sendStreamError(streamId: number, errorMessage: string): void {
867+
_sendStreamError(streamId: number, err: Error): void {
868868
this._subscriptions.delete(streamId);
869869
this._connection.sendOne({
870-
code: ERROR_CODES.APPLICATION_ERROR,
870+
code: err instanceof RSocketError ? err.errorCode : ERROR_CODES.APPLICATION_ERROR,
871871
flags: 0,
872-
message: errorMessage,
872+
message: err.message,
873873
streamId,
874874
type: FRAME_TYPES.ERROR,
875875
});
876-
const error = new Error(`terminated from the requester: ${errorMessage}`);
876+
const error = new Error(`terminated from the requester: ${err.message}`);
877877
this._handleStreamError(streamId, error);
878878
}
879879

@@ -943,3 +943,11 @@ function deserializeMetadataPushPayload<D, M>(
943943
metadata: serializers.metadata.deserialize(frame.metadata),
944944
};
945945
}
946+
947+
export class RSocketError extends Error {
948+
+errorCode: number;
949+
constructor(errorCode: number, message: string) {
950+
super(message);
951+
this.errorCode = errorCode;
952+
}
953+
}

0 commit comments

Comments
 (0)