forked from backstage/backstage
-
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.
Merge pull request backstage#326 from spotify/rugvip/errors
Add initial version of core error API with small showcase in welcome plugin and app
- Loading branch information
Showing
16 changed files
with
357 additions
and
18 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* Copyright 2020 Spotify AB | ||
* | ||
* Licensed 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 { ApiHolder, ApiRegistry, errorApiRef } from '@spotify-backstage/core'; | ||
import { ErrorDisplayForwarder } from './components/ErrorDisplay/ErrorDisplay'; | ||
|
||
const builder = ApiRegistry.builder(); | ||
|
||
export const errorDialogForwarder = new ErrorDisplayForwarder(); | ||
|
||
builder.add(errorApiRef, errorDialogForwarder); | ||
|
||
export default builder.build() as ApiHolder; |
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,87 @@ | ||
/* | ||
* Copyright 2020 Spotify AB | ||
* | ||
* Licensed 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, { FC, useEffect, useState } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { Snackbar } from '@material-ui/core'; | ||
import { Alert } from '@material-ui/lab'; | ||
import { ErrorApi, ErrorContext } from '@spotify-backstage/core'; | ||
|
||
type SubscriberFunc = (error: Error) => void; | ||
type Unsubscribe = () => void; | ||
|
||
// TODO: figure out where to put implementations of APIs, both inside apps | ||
// but also in core/separate package. | ||
export class ErrorDisplayForwarder implements ErrorApi { | ||
private readonly subscribers = new Set<SubscriberFunc>(); | ||
|
||
post(error: Error, context?: ErrorContext) { | ||
if (context?.hidden) { | ||
return; | ||
} | ||
|
||
this.subscribers.forEach(subscriber => subscriber(error)); | ||
} | ||
|
||
subscribe(func: SubscriberFunc): Unsubscribe { | ||
this.subscribers.add(func); | ||
|
||
return () => { | ||
this.subscribers.delete(func); | ||
}; | ||
} | ||
} | ||
|
||
type Props = { | ||
forwarder: ErrorDisplayForwarder; | ||
}; | ||
|
||
// TODO: improve on this and promote to a shared component for use by all apps. | ||
const ErrorDisplay: FC<Props> = ({ forwarder }) => { | ||
const [errors, setErrors] = useState<Array<Error>>([]); | ||
|
||
useEffect(() => { | ||
return forwarder.subscribe(error => setErrors(errs => errs.concat(error))); | ||
}, [forwarder]); | ||
|
||
if (errors.length === 0) { | ||
return null; | ||
} | ||
|
||
const [firstError] = errors; | ||
|
||
const handleClose = () => { | ||
setErrors(errs => errs.filter(err => err !== firstError)); | ||
}; | ||
|
||
return ( | ||
<Snackbar | ||
open | ||
message={firstError.toString()} | ||
anchorOrigin={{ vertical: 'top', horizontal: 'center' }} | ||
> | ||
<Alert onClose={handleClose} severity="error"> | ||
{firstError.toString()} | ||
</Alert> | ||
</Snackbar> | ||
); | ||
}; | ||
|
||
ErrorDisplay.propTypes = { | ||
forwarder: PropTypes.instanceOf(ErrorDisplayForwarder).isRequired, | ||
}; | ||
|
||
export default ErrorDisplay; |
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,17 @@ | ||
/* | ||
* Copyright 2020 Spotify AB | ||
* | ||
* Licensed 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. | ||
*/ | ||
|
||
export { default } from './ErrorDisplay'; |
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
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,62 @@ | ||
/* | ||
* Copyright 2020 Spotify AB | ||
* | ||
* Licensed 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 ApiRef from '../ApiRef'; | ||
|
||
/** | ||
* Mirrors the javascript Error class, for the purpose of | ||
* providing documentation and optional fields. | ||
*/ | ||
type Error = { | ||
name: string; | ||
message: string; | ||
stack?: string; | ||
}; | ||
|
||
/** | ||
* Provides additional information about an error that was posted to the application. | ||
*/ | ||
export type ErrorContext = { | ||
// If set to true, this error should not be displayed to the user. Defaults to false. | ||
hidden?: boolean; | ||
}; | ||
|
||
/** | ||
* The error API is used to report errors to the app, and display them to the user. | ||
* | ||
* Plugins can use this API as a method of displaying errors to the user, but also | ||
* to report errors for collection by error reporting services. | ||
* | ||
* If an error can be displayed inline, e.g. as feedback in a form, that should be | ||
* preferred over relying on this API to display the error. The main use of this api | ||
* for displaying errors should be for asynchronous errors, such as a failing background process. | ||
* | ||
* Even if an error is displayed inline, it should still be reported through this api | ||
* if it would be useful to collect or log it for debugging purposes, but with | ||
* the hidden flag set. For example, an error arising from form field validation | ||
* should probably not be reported, while a failed REST call would be useful to report. | ||
*/ | ||
export type ErrorApi = { | ||
/** | ||
* Post an error for handling by the application. | ||
*/ | ||
post(error: Error, context?: ErrorContext); | ||
}; | ||
|
||
export const errorApiRef = new ApiRef<ErrorApi>({ | ||
id: 'core.error', | ||
description: 'Used to report errors and forward them to the app', | ||
}); |
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 @@ | ||
/* | ||
* Copyright 2020 Spotify AB | ||
* | ||
* Licensed 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. | ||
*/ | ||
|
||
// This folder contains definitions for all core APIs. | ||
// | ||
// Plugins should rely on these APIs for functionality as much as possible. | ||
// | ||
// If you think some API definition is missing, please open an Issue or send a PR! | ||
|
||
export * from './error'; |
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
Oops, something went wrong.