1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-18 09:13:33 +00:00

parse validation errors from error response model

This commit is contained in:
Kyle Spearrin
2019-01-04 23:54:57 -05:00
parent fc5fcb905f
commit 035b4e1dd5
2 changed files with 35 additions and 24 deletions

View File

@@ -23,16 +23,38 @@ export class ErrorResponse {
}
getSingleMessage(): string {
if (this.validationErrors) {
for (const key in this.validationErrors) {
if (!this.validationErrors.hasOwnProperty(key)) {
continue;
}
if (this.validationErrors[key].length) {
return this.validationErrors[key][0];
}
if (this.validationErrors == null) {
return this.message;
}
for (const key in this.validationErrors) {
if (!this.validationErrors.hasOwnProperty(key)) {
continue;
}
if (this.validationErrors[key].length) {
return this.validationErrors[key][0];
}
}
return this.message;
}
getAllMessages(): string[] {
const messages: string[] = [];
if (this.validationErrors == null) {
return messages;
}
for (const key in this.validationErrors) {
if (!this.validationErrors.hasOwnProperty(key)) {
continue;
}
this.validationErrors[key].forEach((item: string) => {
let prefix = '';
if (key.indexOf('[') > -1 && key.indexOf(']') > -1) {
const lastSep = key.lastIndexOf('.');
prefix = key.substr(0, lastSep > -1 ? lastSep : key.length) + ': ';
}
messages.push(prefix + item);
});
}
return messages;
}
}