From 399890c3b8bf6174a981344458b6e552281792b3 Mon Sep 17 00:00:00 2001 From: Charles Wahome Date: Mon, 8 Jun 2020 17:24:42 +0300 Subject: [PATCH 1/3] add json validation for sample queries --- src/app/views/query-runner/QueryRunner.tsx | 25 +++++++++++++++++----- src/types/query-runner.ts | 1 + 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/app/views/query-runner/QueryRunner.tsx b/src/app/views/query-runner/QueryRunner.tsx index 0a868202c..dbfddeb2d 100644 --- a/src/app/views/query-runner/QueryRunner.tsx +++ b/src/app/views/query-runner/QueryRunner.tsx @@ -1,5 +1,6 @@ -import { IDropdownOption } from 'office-ui-fabric-react'; +import { IDropdownOption, MessageBarType } from 'office-ui-fabric-react'; import React, { Component } from 'react'; +import { injectIntl } from 'react-intl'; import { connect } from 'react-redux'; import { bindActionCreators, Dispatch } from 'redux'; @@ -9,6 +10,7 @@ import { } from '../../../types/query-runner'; import * as queryActionCreators from '../../services/actions/query-action-creators'; import * as queryInputActionCreators from '../../services/actions/query-input-action-creators'; +import * as queryStatusActionCreators from '../../services/actions/query-status-action-creator'; import { parseSampleUrl } from '../../utils/sample-url-generation'; import './query-runner.scss'; import QueryInput from './QueryInput'; @@ -66,10 +68,21 @@ export class QueryRunner extends Component< private handleOnRunQuery = () => { const { sampleBody } = this.state; - const { actions, sampleQuery } = this.props; + const { actions, sampleQuery, } = this.props; + const { intl: { messages } }: any = this.props; if (sampleBody) { - sampleQuery.sampleBody = JSON.parse(sampleBody); + try { + sampleQuery.sampleBody = JSON.parse(sampleBody); + } catch (error) { + actions!.setQueryResponseStatus({ + ok: false, + statusText: messages['Malformed JSON body'], + status: error, + messageType: MessageBarType.error + }); + return; + } } if (actions) { @@ -136,7 +149,7 @@ export class QueryRunner extends Component< function mapDispatchToProps(dispatch: Dispatch): object { return { actions: bindActionCreators( - { ...queryActionCreators, ...queryInputActionCreators }, + { ...queryActionCreators, ...queryInputActionCreators, ...queryStatusActionCreators }, dispatch ) }; @@ -148,7 +161,9 @@ function mapStateToProps(state: any) { }; } +// @ts-ignore +const IntlQueryRunner = injectIntl(QueryRunner); export default connect( mapStateToProps, mapDispatchToProps -)(QueryRunner); +)(IntlQueryRunner); diff --git a/src/types/query-runner.ts b/src/types/query-runner.ts index e7e7b6bec..1ee4779cc 100644 --- a/src/types/query-runner.ts +++ b/src/types/query-runner.ts @@ -22,6 +22,7 @@ export interface IQueryRunnerProps { runQuery: Function; addRequestHeader: Function; setSampleQuery: Function; + setQueryResponseStatus: Function; }; } From ce27852712dfb8a38572f18f9c82416e8bb75a6a Mon Sep 17 00:00:00 2001 From: Charles Wahome Date: Mon, 8 Jun 2020 17:24:55 +0300 Subject: [PATCH 2/3] add message to localisation --- src/messages/GE.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/messages/GE.json b/src/messages/GE.json index f6ee5f220..50010049b 100644 --- a/src/messages/GE.json +++ b/src/messages/GE.json @@ -304,5 +304,6 @@ "permissions not found": "We did not find permissions", "selected": "selected", "Search sample queries": "Search sample queries", - "Search history items": "Search history items" + "Search history items": "Search history items", + "Malformed JSON body": "Malformed JSON body" } \ No newline at end of file From 55712f2749dfb64019e25ebe8b4e8e21a0d8a8d1 Mon Sep 17 00:00:00 2001 From: Charles Wahome Date: Tue, 9 Jun 2020 00:29:08 +0300 Subject: [PATCH 3/3] add action to message --- src/app/views/query-runner/QueryRunner.tsx | 4 ++-- src/messages/GE.json | 7 ++++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/app/views/query-runner/QueryRunner.tsx b/src/app/views/query-runner/QueryRunner.tsx index dbfddeb2d..02adcab9d 100644 --- a/src/app/views/query-runner/QueryRunner.tsx +++ b/src/app/views/query-runner/QueryRunner.tsx @@ -68,7 +68,7 @@ export class QueryRunner extends Component< private handleOnRunQuery = () => { const { sampleBody } = this.state; - const { actions, sampleQuery, } = this.props; + const { actions, sampleQuery, } = this.props; const { intl: { messages } }: any = this.props; if (sampleBody) { @@ -78,7 +78,7 @@ export class QueryRunner extends Component< actions!.setQueryResponseStatus({ ok: false, statusText: messages['Malformed JSON body'], - status: error, + status: `${messages['Review the request body']} ${error}`, messageType: MessageBarType.error }); return; diff --git a/src/messages/GE.json b/src/messages/GE.json index 50010049b..5e3b5172f 100644 --- a/src/messages/GE.json +++ b/src/messages/GE.json @@ -290,9 +290,9 @@ "Consent": "Consent", "permissions required to run the query": "The following permissions are required to run the query. To consent to the permissions, click Consent.", "tab": " tab", - "View the":" View the", + "View the": " View the", "viewing a cached set": "You are viewing a cached set of samples because of a network connection failure.", - "see more queries":"See more queries in the", + "see more queries": "See more queries in the", "Microsoft Graph API Reference docs": "Microsoft Graph API Reference docs.", "Fetching permissions": "Fetching permissions", "Authentication failed": "Authentication failed", @@ -305,5 +305,6 @@ "selected": "selected", "Search sample queries": "Search sample queries", "Search history items": "Search history items", - "Malformed JSON body": "Malformed JSON body" + "Malformed JSON body": "Malformed JSON body", + "Review the request body": "Review the request body and fix any malformed JSON." } \ No newline at end of file