Skip to content

Commit

Permalink
Throw an error on non 2xx responses
Browse files Browse the repository at this point in the history
  • Loading branch information
MacroPower committed Jun 15, 2020
1 parent 7674ec0 commit a7e6967
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
35 changes: 24 additions & 11 deletions src/AnalyticsPanel.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { PureComponent } from 'react';
import { Props } from 'types';
import { contextSrv } from 'grafana/app/core/core';
import { isValidUrl, getDomainName, getDate } from './utils';
import { isValidUrl, getDomainName, getDate, throwOnBadResponse } from './utils';
import { PLUGIN_NAME } from './constants';
import { flatten } from 'flat';
import { Button, JSONFormatter, ErrorWithStack } from '@grafana/ui';
Expand Down Expand Up @@ -32,9 +32,11 @@ export class AnalyticsPanel extends PureComponent<Props> {
return result;
};

request = (): RequestInit => {
getRequestInit = (): RequestInit => {
const { noCors } = this.props.options.analyticsOptions;

this.setState({ error: undefined });

return {
method: 'POST',
mode: noCors ? 'no-cors' : 'cors',
Expand All @@ -49,19 +51,22 @@ export class AnalyticsPanel extends PureComponent<Props> {
const { server, postEnd } = this.props.options.analyticsOptions;

if (isValidUrl(server)) {
const req = fetch(server, this.request());
const req = fetch(server, this.getRequestInit());

if (postEnd) {
req
.then(r => throwOnBadResponse(r))
.then(r => r.json())
.then(r => this.setState({ update: r.location }))
.catch((e: Error) => {
this.setState({ error: e });
});
} else {
req.catch((e: Error) => {
this.setState({ error: e });
});
req
.then(r => throwOnBadResponse(r))
.catch((e: Error) => {
this.setState({ error: e });
});
}
} else {
const error = new Error(`"${server}" is not a valid URL`);
Expand All @@ -75,10 +80,12 @@ export class AnalyticsPanel extends PureComponent<Props> {

if (postEnd && update) {
const url = server + '/' + update;
fetch(url, this.request()).catch((e: Error) => {
const error = `${PLUGIN_NAME} final payload error : ${e.name} : ${e.message}`;
console.log(error);
});
fetch(url, this.getRequestInit())
.then(r => throwOnBadResponse(r))
.catch((e: Error) => {
const error = `${PLUGIN_NAME} final payload error : ${e.name} : ${e.message}`;
console.log(error);
});
}
};

Expand All @@ -105,7 +112,13 @@ export class AnalyticsPanel extends PureComponent<Props> {
}}
>
{error && (
<div style={{ display: 'inline-block', textAlign: 'center', width }}>
<div
style={{
display: 'inline-block',
textAlign: 'center',
width: '100%',
}}
>
<ErrorWithStack title={`${PLUGIN_NAME} error`} error={error} errorInfo={null} />
<Button onClick={() => this.sendInitPayload()}>Retry</Button>
</div>
Expand Down
10 changes: 10 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,13 @@ export function getDomainName(str: string) {
export function getDate() {
return Math.floor(new Date().getTime() / 1000);
}

export function throwOnBadResponse(r: Response) {
const status = r.status.toString();
const regExp = /^(0)|(20[0-4])$/;

if (!regExp.test(status)) {
throw new Error(`Returned status ${status}`);
}
return r;
}

0 comments on commit a7e6967

Please sign in to comment.