Skip to content

Commit

Permalink
Merge pull request #1079 from versionpress/1074-invalid-response-hand…
Browse files Browse the repository at this point in the history
…ling

Improve invalid REST API response handling
  • Loading branch information
JanVoracek committed Jul 4, 2016
2 parents d6d76f4 + d163fa3 commit f423bab
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 16 deletions.
2 changes: 1 addition & 1 deletion frontend/src/CommitPanel/CommitPanelOverview.react.tsx
Expand Up @@ -3,7 +3,7 @@
import * as React from 'react';

interface CommitPanelOverviewProps extends React.Props<JSX.Element> {
gitStatus: string[][];
gitStatus: VpApi.GetGitStatusResponse;
}

interface CommitPanelOverviewState {
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/Commits/revertDialog.tsx
Expand Up @@ -42,7 +42,8 @@ export function revertDialog(title: React.ReactNode, okHandler: Function) {
const req = WpApi
.get('can-revert')
.end((err: any, res: request.Response) => {
if (res.body) {
const data = res.body.data as VpApi.CanRevertResponse;
if (data === true) {
const body = <UndoEnabledDialog />;
portal.confirmDialog(title, body, okHandler, () => {}, {});
} else {
Expand Down
21 changes: 13 additions & 8 deletions frontend/src/pages/HomePage.react.tsx
Expand Up @@ -82,7 +82,7 @@ export default class HomePage extends React.Component<HomePageProps, HomePageSta

static getErrorMessage(res: request.Response, err: any) {
if (res) {
const body = Array.isArray(res.body) ? res.body[0] : res.body;
const body = res.body;
if ('code' in body && 'message' in body) {
return body;
}
Expand Down Expand Up @@ -125,6 +125,7 @@ export default class HomePage extends React.Component<HomePageProps, HomePageSta
.query({page: page, query: encodeURIComponent(this.state.query)})
.on('progress', (e) => progressBar.progress(e.percent))
.end((err: any, res: request.Response) => {
const data = res.body.data as VpApi.GetCommitsResponse;
if (err) {
this.setState({
pages: [],
Expand All @@ -135,8 +136,8 @@ export default class HomePage extends React.Component<HomePageProps, HomePageSta
});
} else {
this.setState({
pages: res.body.pages.map(c => c + 1),
commits: res.body.commits as Commit[],
pages: data.pages.map(c => c + 1),
commits: data.commits,
message: null,
loading: false,
displayUpdateNotice: false
Expand All @@ -150,10 +151,11 @@ export default class HomePage extends React.Component<HomePageProps, HomePageSta
WpApi
.get('display-welcome-panel')
.end((err: any, res: request.Response) => {
const data = res.body.data as VpApi.DisplayWelcomePanelResponse;
if (err) {
return;
}
if (res.body[0] === true) {
if (data === true) {
this.setState({displayWelcomePanel: true});
} else {
this.setState({displayWelcomePanel: false});
Expand All @@ -169,6 +171,7 @@ export default class HomePage extends React.Component<HomePageProps, HomePageSta
.get('should-update')
.query({query: encodeURIComponent(this.state.query), latestCommit: this.state.commits[0].hash})
.end((err: any, res: request.Response) => {
const data = res.body.data as VpApi.ShouldUpdateResponse;
if (err) {
this.setState({
displayUpdateNotice: false,
Expand All @@ -177,8 +180,8 @@ export default class HomePage extends React.Component<HomePageProps, HomePageSta
clearInterval(this.refreshInterval);
} else {
this.setState({
displayUpdateNotice: !this.props.params.page && res.body.update === true,
dirtyWorkingDirectory: res.body.cleanWorkingDirectory !== true
displayUpdateNotice: !this.props.params.page && data.update === true,
dirtyWorkingDirectory: data.cleanWorkingDirectory !== true
});
}
});
Expand Down Expand Up @@ -227,10 +230,11 @@ export default class HomePage extends React.Component<HomePageProps, HomePageSta
WpApi
.get('git-status')
.end((err, res: request.Response) => {
const data = res.body.data as VpApi.GetGitStatusResponse;
if (err) {
reject(HomePage.getErrorMessage(res, err));
} else {
resolve(res.body);
resolve(data);
}
});
});
Expand All @@ -243,10 +247,11 @@ export default class HomePage extends React.Component<HomePageProps, HomePageSta
.get('diff')
.query(query)
.end((err, res: request.Response) => {
const data = res.body.data as VpApi.GetDiffResponse;
if (err) {
reject(HomePage.getErrorMessage(res, err));
} else {
resolve(res.body.diff);
resolve(data.diff);
}
});
});
Expand Down
25 changes: 25 additions & 0 deletions frontend/src/services/VpApi.d.ts
@@ -0,0 +1,25 @@
declare module VpApi {
interface GetCommitsResponse {
pages: number[];
commits: Commit[];
}

type UndoCommitsResponse = boolean;

type RollbackToCommitResponse = boolean;

type CanRevertResponse = boolean;

interface GetDiffResponse {
diff: string;
}

type DisplayWelcomePanelResponse = boolean;

interface ShouldUpdateResponse {
update: boolean;
cleanWorkingDirectory: boolean;
}

type GetGitStatusResponse = string[][];
}
1 change: 1 addition & 0 deletions frontend/src/services/WpApi.ts
@@ -1,4 +1,5 @@
/// <reference path='../../typings/typings.d.ts' />
/// <reference path='./Wp.d.ts' />

import * as request from 'superagent';
import config from '../config';
Expand Down
12 changes: 6 additions & 6 deletions plugins/versionpress/src/Api/VersionPressApi.php
Expand Up @@ -186,21 +186,21 @@ private function handleErrorOutput($routeHandler)
$data = ($response instanceof WP_Error)
? $response->get_error_data()
: $response->get_data();
if (!is_array($data)) {
$data = [$data];
}

$data['__VP__'] = true;
$responseArr = [
'__VP__' => true,
'data' => $data
];

if (ob_get_length() > 0) {
$bufferContents = ob_get_clean();
$data['phpBuffer'] = $bufferContents;
}

if ($response instanceof WP_Error) {
$response->add_data($data);
$response->add_data($responseArr);
} else {
$response->set_data($data);
$response->set_data($responseArr);
}

return $response;
Expand Down

0 comments on commit f423bab

Please sign in to comment.