From e20eff6f168a335440981e7323175f2afaa62926 Mon Sep 17 00:00:00 2001 From: alisahinozcelik Date: Wed, 29 Nov 2023 18:56:07 +0300 Subject: [PATCH] feat(Core): added error handling to message client --- src/index.ts | 1 + src/message-response.type.ts | 7 +++++-- src/request.decorator.ts | 12 ++++++++++-- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/index.ts b/src/index.ts index a5b7c19..6cf2504 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,3 +4,4 @@ export { Message } from './message.interface'; export { MessageResponse } from './message-response.type'; export { MessageClient } from './message-client'; export { MessageHost } from './message-host'; +export * from './selectors'; diff --git a/src/message-response.type.ts b/src/message-response.type.ts index 7b71c96..112baf7 100644 --- a/src/message-response.type.ts +++ b/src/message-response.type.ts @@ -2,7 +2,10 @@ import { Message } from './message.interface'; type Omit = Pick>; -type UncompletedMessageResponse = Omit, 'path'> & { completed: false; }; +export type UncompletedMessageResponse = Omit, 'path'> & { completed: false; }; type CompletedMessageResponse = Omit & { completed: true; }; -export type MessageResponse = UncompletedMessageResponse | CompletedMessageResponse; +export type ErrorMessageResponse = {error: any}; +export type SuccessfulMessageResponse = UncompletedMessageResponse | CompletedMessageResponse; + +export type MessageResponse = SuccessfulMessageResponse | ErrorMessageResponse; diff --git a/src/request.decorator.ts b/src/request.decorator.ts index 4bf2f2e..b164ab4 100644 --- a/src/request.decorator.ts +++ b/src/request.decorator.ts @@ -1,7 +1,8 @@ -import { filter, pluck, takeWhile } from 'rxjs/operators'; +import { filter, map, takeWhile } from 'rxjs/operators'; import { MessageClient } from './message-client'; import { GET_NEW_ID, RESPONSES$, SEND } from './selectors'; +import { ErrorMessageResponse, SuccessfulMessageResponse, UncompletedMessageResponse } from './message-response.type'; export function Request(path: string): MethodDecorator { return function(target: object, key: string, descriptor: TypedPropertyDescriptor): TypedPropertyDescriptor { @@ -11,9 +12,16 @@ export function Request(path: string): MethodDecorator { this[SEND]({body: message, id: messageId, path}); return this[RESPONSES$].pipe( + map((data) => { + const {error} = data as ErrorMessageResponse; + + if (error) throw error; + + return data as SuccessfulMessageResponse; + }), filter(({id}) => id === messageId), takeWhile(({completed}) => !completed), - pluck('body'), + map(({body}: UncompletedMessageResponse) => body), ); };