mirror of
https://github.com/grafana/grafana.git
synced 2025-02-11 08:05:43 -06:00
Dashboards: Provide better error messages in SaveDashboardAsForm (#57866)
The existing code uses `instanceof Error` to check for a `message` field on the thrown object. The objects that are thrown are never instances of the error interface. This change introduces a new type that extends Error so that the check works properly and displays a meaningful error message in the UI.
This commit is contained in:
parent
a255c32e1a
commit
f07da85d8b
@ -5,6 +5,15 @@ const hitTypes = {
|
|||||||
DASHBOARD: 'dash-db',
|
DASHBOARD: 'dash-db',
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class ValidationError extends Error {
|
||||||
|
type: string;
|
||||||
|
|
||||||
|
constructor(type: string, message: string) {
|
||||||
|
super(message);
|
||||||
|
this.type = type;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export class ValidationSrv {
|
export class ValidationSrv {
|
||||||
rootName = 'general';
|
rootName = 'general';
|
||||||
|
|
||||||
@ -21,17 +30,11 @@ export class ValidationSrv {
|
|||||||
const nameLowerCased = name.toLowerCase();
|
const nameLowerCased = name.toLowerCase();
|
||||||
|
|
||||||
if (name.length === 0) {
|
if (name.length === 0) {
|
||||||
throw {
|
throw new ValidationError('REQUIRED', 'Name is required');
|
||||||
type: 'REQUIRED',
|
|
||||||
message: 'Name is required',
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (folderId === 0 && nameLowerCased === this.rootName) {
|
if (folderId === 0 && nameLowerCased === this.rootName) {
|
||||||
throw {
|
throw new ValidationError('EXISTING', 'This is a reserved name and cannot be used for a folder.');
|
||||||
type: 'EXISTING',
|
|
||||||
message: 'This is a reserved name and cannot be used for a folder.',
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const promises = [];
|
const promises = [];
|
||||||
@ -51,10 +54,7 @@ export class ValidationSrv {
|
|||||||
|
|
||||||
for (const hit of hits) {
|
for (const hit of hits) {
|
||||||
if (nameLowerCased === hit.title.toLowerCase()) {
|
if (nameLowerCased === hit.title.toLowerCase()) {
|
||||||
throw {
|
throw new ValidationError('EXISTING', existingErrorMessage);
|
||||||
type: 'EXISTING',
|
|
||||||
message: existingErrorMessage,
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user