From f973bdd4558a1b14a058c9f0014641b4a1dbfd7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20B=C3=BCchele?= Date: Thu, 18 Oct 2018 02:45:28 -0700 Subject: [PATCH] add notification API to network plugin Summary: Trigger notifications for all network requests having an error status code. Reviewed By: passy Differential Revision: D10401402 fbshipit-source-id: 8fa63cbc251457142abd71845fb6f1c735b247cc --- src/plugins/network/index.js | 43 ++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/src/plugins/network/index.js b/src/plugins/network/index.js index 4bb6277f2..b8d173821 100644 --- a/src/plugins/network/index.js +++ b/src/plugins/network/index.js @@ -141,6 +141,28 @@ export default class extends FlipperPlugin { }; }; + static getActiveNotifications = ( + persistedState: PersistedState, + ): Array => { + const responses = persistedState ? persistedState.responses || [] : []; + return ( + // $FlowFixMe Object.values returns Array, but we know it is Array + (Object.values(responses): Array) + // Show error messages for all status codes indicating a client or server error + .filter((response: Response) => response.status >= 400) + .map((response: Response) => ({ + id: response.id, + title: `HTTP ${response.status}: Network request failed`, + message: `Request to "${persistedState.requests[response.id]?.url || + '(URL missing)'}" failed. ${response.reason}`, + severity: 'error', + timestamp: response.timestamp, + category: response.status, + action: response.id, + })) + ); + }; + onKeyboardAction = (action: string) => { if (action === 'clear') { this.clearLogs(); @@ -159,27 +181,6 @@ export default class extends FlipperPlugin { this.props.setPersistedState({responses: {}, requests: {}}); }; - computeNotifications(props: *, state: State) { - const notifications: Array = []; - const persistedState = props.persistedState; - for (const response in persistedState.responses) { - const status = persistedState.responses[response].status; - const id = persistedState.requests[response]?.id; - if (status >= 400 && id != null) { - const url = persistedState.requests[response]?.url; - const endTime = persistedState.responses[response].timestamp; - notifications.push({ - id, - timestamp: endTime, - title: 'Failed network request', - message: `Response for ${url} failed with status code ${status}`, - severity: 'error', - }); - } - } - return notifications; - } - renderSidebar = () => { const {requests, responses} = this.props.persistedState; const {selectedIds} = this.state;