mirror of
https://github.com/hashicorp/vault.git
synced 2026-02-15 12:51:58 +01:00
* adds codemirror types * adds api error message util * converts tools/wrap component to ts and updates wrap request to use api service * adds comments to api and error message utils * adds type to html element event import
29 lines
1.1 KiB
TypeScript
29 lines
1.1 KiB
TypeScript
/**
|
|
* Copyright (c) HashiCorp, Inc.
|
|
* SPDX-License-Identifier: BUSL-1.1
|
|
*/
|
|
|
|
/**
|
|
* this util was derived from error-message and updated to handle the error context returned from the api service
|
|
* once Ember Data is fully removed, the error-message util will also be removed
|
|
* for all requests made with the api service, use this util to display error messages from server
|
|
*/
|
|
|
|
import { ErrorContext, ApiError } from 'vault/api';
|
|
|
|
// accepts an error and returns error.errors joined with a comma, error.message or a fallback message
|
|
export default async function (error: unknown, fallbackMessage = 'An error occurred, please try again') {
|
|
const messageOrFallback = (message?: string) => message || fallbackMessage;
|
|
|
|
if ((error as ErrorContext).response instanceof Response) {
|
|
const apiError: ApiError = await (error as ErrorContext).response?.json();
|
|
|
|
if (apiError.errors && typeof apiError.errors[0] === 'string') {
|
|
return apiError.errors.join(', ');
|
|
}
|
|
return messageOrFallback(apiError.message);
|
|
}
|
|
|
|
return messageOrFallback((error as Error)?.message);
|
|
}
|