-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathmessage-error.js
50 lines (45 loc) · 1.47 KB
/
message-error.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: BUSL-1.1
*/
import Component from '@glimmer/component';
import layout from '../templates/components/message-error';
import { setComponentTemplate } from '@ember/component';
/**
* @module MessageError
* Renders form errors using the Hds::Alert component and extracts errors from
* a model, passed errorMessage or array of errors and displays each in a separate banner.
*
* @example
* <MessageError @errorMessage="oh no there is a problem" />
*
* @param {object} [model=null] - An Ember data model that will be used to bind error states to the internal `errors` property.
* @param {array} [errors=null] - An array of error strings to show.
* @param {string} [errorMessage=null] - An Error string to display.
*/
class MessageError extends Component {
get displayErrors() {
const { errorMessage, errors, model } = this.args;
if (errorMessage) {
return [errorMessage];
}
if (errors && errors.length > 0) {
return errors;
}
if (model?.isError) {
const adapterError = model?.adapterError;
if (!adapterError) {
return null;
}
if (adapterError.errors?.length > 0) {
return adapterError.errors.map((e) => {
if (typeof e === 'object') return e.title || e.message || JSON.stringify(e);
return e;
});
}
return [adapterError.message];
}
return null;
}
}
export default setComponentTemplate(layout, MessageError);