vault/ui/app/utils/api-error-message.ts
Jordan Reimer e18b0485f5
[UI] Ember Data Migration - Config UI Engine (#30238)
* WIP updating config-ui engine to use api service and form class

* updates form-field component to support false values for radio types

* updates api-error-message util to log out error in dev env

* fixes issues in custom messages create and edit workflows

* fixes issues in api service

* updates capabilities handling

* updates to custom messages form

* removes store from custom messages tests

* removes store as dependency from config-ui engine

* removes commented out code in messages route

* updates orderedKeys to displayFields in messages page component

* removes unneccesary method var from message create-and-edit component

* removes comment about model in message details page
2025-04-28 16:09:11 -06:00

35 lines
1.3 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';
import ENV from 'vault/config/environment';
// 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;
// log out the error for ease of debugging in dev env
if (ENV.environment === 'development') {
console.error('API Error:', error); // eslint-disable-line no-console
}
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);
}