From cab29ba509b49bf71c5a744463e17230f29ad27b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maren=20S=C3=BCwer?= Date: Thu, 25 Oct 2018 14:24:53 +0200 Subject: [PATCH] add error handling --- .../src/repos/sources/containers/Content.js | 39 ++++++++++++++----- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/scm-ui/src/repos/sources/containers/Content.js b/scm-ui/src/repos/sources/containers/Content.js index 3518a03468..c5b64321f4 100644 --- a/scm-ui/src/repos/sources/containers/Content.js +++ b/scm-ui/src/repos/sources/containers/Content.js @@ -31,7 +31,9 @@ type Props = { }; type State = { - contentType: string + contentType: string, + error: Error, + hasError: boolean }; class Content extends React.Component { @@ -39,25 +41,44 @@ class Content extends React.Component { super(props); this.state = { - contentType: "" + contentType: "", + error: new Error(), + hasError: false }; } componentDidMount() { const { file } = this.props; - getContentType(file._links.self.href).then(result => { - this.setState({ - contentType: result - }); - }); + getContentType(file._links.self.href) + .then(result => { + if (result.error) { + this.setState({ + ...this.state, + hasError: true, + error: result.error + }); + } else { + this.setState({ + ...this.state, + contentType: result.type + }); + } + }) + .catch(err => {}); } render() { const { file } = this.props; const contentType = this.state.contentType; + const error = this.state.error; + const hasError = this.state.hasError; + if (!file) { return ; } + if (hasError) { + return ; + } if (contentType.startsWith("image")) { return ; } @@ -74,10 +95,10 @@ export function getContentType(url: string, state: any) { return apiClient .head(url) .then(response => { - return response.headers.get("Content-Type"); + return { type: response.headers.get("Content-Type") }; }) .catch(err => { - return null; + return { error: err }; }); }