forked from opensearch-project/OpenSearch-Dashboards
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: allow graceful error handling (opensearch-project#779)
- Loading branch information
1 parent
5c68eec
commit b201445
Showing
6 changed files
with
135 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
packages/osd-charts/src/components/error_boundary/error_boundary.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import React, { Component, ReactNode } from 'react'; | ||
|
||
import { isGracefulError } from './errors'; | ||
|
||
type ErrorBoundaryProps = { | ||
children: ReactNode; | ||
}; | ||
|
||
interface ErrorBoundaryState { | ||
hasError: boolean; | ||
} | ||
|
||
/** | ||
* Error Boundary to catch and handle custom errors | ||
* @internal | ||
*/ | ||
export class ErrorBoundary extends Component<ErrorBoundaryProps, ErrorBoundaryState> { | ||
hasError = false; | ||
|
||
componentDidUpdate() { | ||
if (this.hasError) { | ||
this.hasError = false; | ||
} | ||
} | ||
|
||
componentDidCatch(error: Error) { | ||
if (isGracefulError(error)) { | ||
this.hasError = true; | ||
this.forceUpdate(); | ||
} | ||
} | ||
|
||
render() { | ||
if (this.hasError) { | ||
return ( | ||
<div className="echReactiveChart_unavailable"> | ||
<p>No data to display</p> | ||
</div> | ||
); | ||
} | ||
|
||
return this.props.children; | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
packages/osd-charts/src/components/error_boundary/errors.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import { $Values } from 'utility-types'; | ||
|
||
export const ErrorType = Object.freeze({ | ||
Graceful: 'graceful' as const, | ||
}); | ||
/** @public */ | ||
export type ErrorType = $Values<typeof ErrorType>; | ||
|
||
/** | ||
* Error to used to gracefully render empty chart | ||
*/ | ||
export class GracefulError extends Error { | ||
type = ErrorType.Graceful; | ||
} | ||
|
||
export function isGracefulError(error: Error): error is GracefulError { | ||
return (error as GracefulError)?.type === ErrorType.Graceful; | ||
} |
23 changes: 23 additions & 0 deletions
23
packages/osd-charts/src/components/error_boundary/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
/* @internal */ | ||
export { ErrorBoundary } from './error_boundary'; | ||
/* @internal */ | ||
export * from './errors'; |