Unverified Commit ba1d6dff authored by Evgeny Stepanovych's avatar Evgeny Stepanovych Committed by GitHub
Browse files

NAS-119438: Showing errors in rsync form (#7475)

parent 69dd88ef
Showing with 28 additions and 9 deletions
+28 -9
import { Injectable } from '@angular/core';
import { UntypedFormGroup } from '@angular/forms';
import { AbstractControl, UntypedFormGroup } from '@angular/forms';
import { ResponseErrorType } from 'app/enums/response-error-type.enum';
import { Job } from 'app/interfaces/job.interface';
import { WebsocketError } from 'app/interfaces/websocket-error.interface';
......@@ -12,14 +12,24 @@ export class FormErrorHandlerService {
private dialog: DialogService,
) {}
handleWsFormError(error: WebsocketError | Job, formGroup: UntypedFormGroup): void {
/**
* @param error
* @param formGroup
* @param fieldsMap Overrides backend field names with frontend field names.
* TODO: See if second `string` in fieldsMap can be typed to key of formGroup.
*/
handleWsFormError(
error: WebsocketError | Job,
formGroup: UntypedFormGroup,
fieldsMap: Record<string, string> = {},
): void {
if ('type' in error && error.type === ResponseErrorType.Validation && error.extra) {
this.handleValidationError(error, formGroup);
this.handleValidationError(error, formGroup, fieldsMap);
return;
}
if ('exc_info' in error && error.exc_info.type === ResponseErrorType.Validation && error.exc_info.extra) {
this.handleValidationError({ ...error, extra: error.exc_info.extra as Job['extra'] }, formGroup);
this.handleValidationError({ ...error, extra: error.exc_info.extra as Job['extra'] }, formGroup, fieldsMap);
return;
}
......@@ -27,14 +37,16 @@ export class FormErrorHandlerService {
(new EntityUtils()).errorReport(error, this.dialog);
}
// TODO: Add support for api fields having different names than formgroup fields, i.e. private_key => privateKey
// TODO: Same for nested API objects.
private handleValidationError(error: WebsocketError | Job, formGroup: UntypedFormGroup): void {
private handleValidationError(
error: WebsocketError | Job,
formGroup: UntypedFormGroup,
fieldsMap: Record<string, string>,
): void {
for (const extraItem of (error as WebsocketError).extra) {
const field = extraItem[0].split('.')[1];
const errorMessage = extraItem[1];
const control = formGroup.get(field);
const control = this.getFormField(formGroup, field, fieldsMap);
if (!control) {
console.error(`Could not find control ${field}.`);
// Fallback to default modal error message.
......@@ -50,4 +62,9 @@ export class FormErrorHandlerService {
control.markAsTouched();
}
}
private getFormField(formGroup: UntypedFormGroup, field: string, fieldsMap: Record<string, string>): AbstractControl {
const fieldName = fieldsMap[field] ?? field;
return formGroup.get(fieldName);
}
}
......@@ -212,7 +212,9 @@ export class RsyncTaskFormComponent implements OnInit {
},
error: (error) => {
this.isLoading = false;
this.errorHandler.handleWsFormError(error, this.form);
this.errorHandler.handleWsFormError(error, this.form, {
remotehost: 'remotepath',
});
this.cdr.markForCheck();
},
});
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment