From bf68b6fa1df4f9aabb78070b285b70ec9e721fa9 Mon Sep 17 00:00:00 2001 From: utkarsha-deriv Date: Fri, 22 Dec 2023 16:47:52 +0400 Subject: [PATCH 1/2] fix: error message JSON body was not displaying --- src/features/Apiexplorer/SubscribeRenderer/index.tsx | 7 ++++++- src/hooks/useSubscription/index.tsx | 9 ++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/features/Apiexplorer/SubscribeRenderer/index.tsx b/src/features/Apiexplorer/SubscribeRenderer/index.tsx index 912a90f5..a107da74 100644 --- a/src/features/Apiexplorer/SubscribeRenderer/index.tsx +++ b/src/features/Apiexplorer/SubscribeRenderer/index.tsx @@ -32,7 +32,12 @@ function SubscribeRenderer({ const [is_not_valid, setIsNotValid] = useState(false); useEffect(() => { - if (error && error.code === 'AuthorizationRequired') { + if ( + error && + typeof error === 'object' && + 'code' in error && + error.code === 'AuthorizationRequired' + ) { setToggleModal(true); } }, [error]); diff --git a/src/hooks/useSubscription/index.tsx b/src/hooks/useSubscription/index.tsx index 06b25cbd..1051a86a 100644 --- a/src/hooks/useSubscription/index.tsx +++ b/src/hooks/useSubscription/index.tsx @@ -6,15 +6,10 @@ import { } from '@site/src/configs/websocket/types'; import { useCallback, useState } from 'react'; -type TError = { - code?: string; - message?: string; -}; - const useSubscription = (name: T) => { const [is_loading, setIsLoading] = useState(false); const [is_subscribed, setSubscribed] = useState(false); - const [error, setError] = useState(); + const [error, setError] = useState(); const [data, setData] = useState>(); const [full_response, setFullResponse] = useState>(); const [subscriber, setSubscriber] = useState<{ unsubscribe?: VoidFunction }>(); @@ -30,7 +25,7 @@ const useSubscription = (name: T) => ); const onError = useCallback((response: TSocketResponse) => { - setError(response.error); + setError(response); setIsLoading(false); setFullResponse(null); }, []); From c9ac0cd2c04f4d74f3a08e37eff21027eb19507a Mon Sep 17 00:00:00 2001 From: utkarsha-deriv Date: Mon, 25 Dec 2023 10:44:29 +0400 Subject: [PATCH 2/2] fix: fix broken testcase --- .../__tests__/useSubscription.test.tsx | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/hooks/useSubscription/__tests__/useSubscription.test.tsx b/src/hooks/useSubscription/__tests__/useSubscription.test.tsx index d3e9c250..d19080de 100644 --- a/src/hooks/useSubscription/__tests__/useSubscription.test.tsx +++ b/src/hooks/useSubscription/__tests__/useSubscription.test.tsx @@ -206,7 +206,22 @@ describe('Use WS', () => { }, }); - expect(result.current.error).toEqual({ message: 'this is an error' }); + expect(result.current.error).toEqual({ + echo_req: { + base_currency: 'USD', + exchange_rates: 1, + req_id: 1, + subscribe: 1, + }, + error: { + message: 'this is an error', + }, + msg_type: 'exchange_rates', + req_id: 1, + subscription: { + id: '2f39f7e7-d986-33f2-080a-b0ee50cfb85c', + }, + }); expect(result.current.is_loading).toBeFalsy(); }); });