mirror of
https://github.com/bitwarden/browser.git
synced 2025-02-08 00:01:28 +01:00
parse validation errors from error response model
This commit is contained in:
parent
fc5fcb905f
commit
035b4e1dd5
@ -3,6 +3,8 @@ import { Injectable } from '@angular/core';
|
||||
import { I18nService } from '../../abstractions/i18n.service';
|
||||
import { PlatformUtilsService } from '../../abstractions/platformUtils.service';
|
||||
|
||||
import { ErrorResponse } from '../../models/response/errorResponse';
|
||||
|
||||
@Injectable()
|
||||
export class ValidationService {
|
||||
constructor(private i18nService: I18nService, private platformUtilsService: PlatformUtilsService) { }
|
||||
@ -15,23 +17,10 @@ export class ValidationService {
|
||||
errors.push(data);
|
||||
} else if (data == null || typeof data !== 'object') {
|
||||
errors.push(defaultErrorMessage);
|
||||
} else if (data.validationErrors == null) {
|
||||
errors.push(data.message ? data.message : defaultErrorMessage);
|
||||
} else if (data.validationErrors != null) {
|
||||
errors.concat((data as ErrorResponse).getAllMessages());
|
||||
} else {
|
||||
for (const key in data.validationErrors) {
|
||||
if (!data.validationErrors.hasOwnProperty(key)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
data.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) + ': ';
|
||||
}
|
||||
errors.push(prefix + item);
|
||||
});
|
||||
}
|
||||
errors.push(data.message ? data.message : defaultErrorMessage);
|
||||
}
|
||||
|
||||
if (errors.length === 1) {
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user