Skip to content

Commit

Permalink
fix: show error if check update/save fails (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
rdubrock committed Aug 11, 2020
1 parent e995db8 commit ec745d8
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 14 deletions.
44 changes: 30 additions & 14 deletions src/components/CheckEditor.tsx
@@ -1,4 +1,5 @@
import React, { PureComponent } from 'react';
import { css } from 'emotion';
import {
Button,
Container,
Expand All @@ -11,9 +12,10 @@ import {
Select,
Legend,
Collapse,
Alert,
} from '@grafana/ui';
import { SelectableValue } from '@grafana/data';
import { Check, Label as SMLabel, Settings, CheckType, Probe, OrgRole } from 'types';
import { Check, Label as SMLabel, Settings, CheckType, Probe, OrgRole, APIError } from 'types';
import { SMDataSource } from 'datasource/DataSource';
import { hasRole, checkType, defaultSettings } from 'utils';
import { PingSettingsForm } from './PingSettings';
Expand All @@ -36,6 +38,7 @@ interface State {
probes: Probe[];
showDeleteModal: boolean;
showOptions: boolean;
error?: APIError;
}

export class CheckEditor extends PureComponent<Props, State> {
Expand Down Expand Up @@ -131,19 +134,21 @@ export class CheckEditor extends PureComponent<Props, State> {
onSave = async () => {
const { instance } = this.props;
const { check } = this.state;
if (!check) {
return;
}
if (check.id) {
console.log('UPDATE', check, instance);
const info = await instance.updateCheck(check);
console.log('got', info);
} else {
console.log('ADD', check);
const info = await instance.addCheck(check);
console.log('got', info);
try {
if (check.id) {
await instance.updateCheck(check);
} else {
await instance.addCheck(check);
}
this.props.onReturn(true);
} catch (e) {
this.setState({
error: {
status: e.status,
message: e.data?.message ?? 'Something went wrong',
},
});
}
this.props.onReturn(true);
};

onEnableChange = (event: React.ChangeEvent<HTMLInputElement>) => {
Expand All @@ -161,7 +166,7 @@ export class CheckEditor extends PureComponent<Props, State> {
};

render() {
const { check, showDeleteModal, probes, typeOfCheck, showOptions } = this.state;
const { check, showDeleteModal, probes, typeOfCheck, showOptions, error } = this.state;
if (!check || probes.length === 0) {
return <div>Loading...</div>;
}
Expand Down Expand Up @@ -317,6 +322,17 @@ export class CheckEditor extends PureComponent<Props, State> {
/>
<a onClick={this.onBack}>Back</a>
</HorizontalGroup>
{error && (
<div
className={css`
margin-top: 1rem;
`}
>
<Alert title="Save failed" severity="error">
{`${error.status}: ${error.message}`}
</Alert>
</div>
)}
</div>
);
}
Expand Down
5 changes: 5 additions & 0 deletions src/types.ts
Expand Up @@ -226,3 +226,8 @@ export enum OrgRole {
EDITOR = 'Editor',
VIEWER = 'Viewer',
}

export interface APIError {
status: number;
message: string;
}

0 comments on commit ec745d8

Please sign in to comment.