Skip to content

Commit

Permalink
Dashboards: Provide better error messages in SaveDashboardAsForm (#57866
Browse files Browse the repository at this point in the history
)

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.

(cherry picked from commit f07da85)
  • Loading branch information
joeblubaugh authored and grafanabot committed Nov 8, 2022
1 parent d9a6bf9 commit f7eb15f
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions public/app/features/manage-dashboards/services/ValidationSrv.ts
Expand Up @@ -5,6 +5,15 @@ const hitTypes = {
DASHBOARD: 'dash-db',
};

class ValidationError extends Error {
type: string;

constructor(type: string, message: string) {
super(message);
this.type = type;
}
}

export class ValidationSrv {
rootName = 'general';

Expand All @@ -21,17 +30,11 @@ export class ValidationSrv {
const nameLowerCased = name.toLowerCase();

if (name.length === 0) {
throw {
type: 'REQUIRED',
message: 'Name is required',
};
throw new ValidationError('REQUIRED', 'Name is required');
}

if (folderId === 0 && nameLowerCased === this.rootName) {
throw {
type: 'EXISTING',
message: 'This is a reserved name and cannot be used for a folder.',
};
throw new ValidationError('EXISTING', 'This is a reserved name and cannot be used for a folder.');
}

const promises = [];
Expand All @@ -51,10 +54,7 @@ export class ValidationSrv {

for (const hit of hits) {
if (nameLowerCased === hit.title.toLowerCase()) {
throw {
type: 'EXISTING',
message: existingErrorMessage,
};
throw new ValidationError('EXISTING', existingErrorMessage);
}
}

Expand Down

0 comments on commit f7eb15f

Please sign in to comment.