I m still learning JHipster, So today I though about making somme validation exerices by my self, and to try to send meaningfull error messages to my frontend
Here is what I tried
In my controller I have the following :
/**
* POST /lessons : Create a new lesson of 45 min.
*
* if lesson is of type creno or circulation the car is mandatory
*
* @param lessonDTO the lessonDTO to create
* @return the ResponseEntity with status 201 (Created) and with body the new lessonDTO, or with status 400 (Bad Request) if the lesson has already an ID
* @throws URISyntaxException if the Location URI syntax is incorrect
*/
@PostMapping("/lessons")
public ResponseEntity<LessonDTO> createLesson(@Valid @RequestBody LessonDTO lessonDTO) throws URISyntaxException {
log.debug("REST request to save Lesson : {}", lessonDTO);
if (lessonDTO.getId() != null) {
throw new BadRequestAlertException("A new lesson cannot already have an ID", ENTITY_NAME, "idexists");
}
if(!lessonService.checkLessonTime(lessonDTO)){
return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert(ENTITY_NAME,"EK_L_C01", "erreur de requete dupliquer")).build();
}
LessonDTO result = lessonService.save(lessonDTO);
return ResponseEntity.created(new URI("/api/lessons/" + result.getId()))
.headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
.body(result);
}
as you see if the check lesson time fails I have to send a bad request response with failure code EK_L_C01 then my next step ws to make a little change in the front end where I got to make this
save() {
this.isSaving = true;
this.lesson.dateLesson = this.dateLesson != null ? moment(this.dateLesson, DATE_TIME_FORMAT) : null;
if (this.lesson.id !== undefined) {
this.subscribeToSaveResponse(this.lessonService.update(this.lesson));
} else {
this.subscribeToSaveResponse(this.lessonService.create(this.lesson));
}
}
protected subscribeToSaveResponse(result: Observable<HttpResponse<ILesson>>) {
result.subscribe((res: HttpResponse<ILesson>) => this.onSaveSuccess(), (res: HttpErrorResponse) => this.onSaveError(res.message));
}
protected onSaveSuccess() {
this.isSaving = false;
this.previousState();
}
protected onSaveError(errorMessage: string) {
this.isSaving = false;
this.onError(errorMessage);
}
protected onError(errorMessage: string) {
this.jhiAlertService.error(errorMessage, null, null);
}
then I added the translation of the code in my global.json as below
"error": {
"internalServerError": "Erreur interne du serveur",
"server.not.reachable": "Serveur inaccessible",
"url.not.found": "Non trouvé",
"NotNull": "Le champ {{fieldName}} ne peut pas être vide !",
"Size": "Le champ {{fieldName}} ne respecte pas les critères minimum et maximum !",
"userexists": "Login déjà utilisé !",
"emailexists": "Email déjà utilisé !",
"idexists": "Une nouvelle entité {{entityName}} ne peut pas avoir d'ID !",
"idnull": "Invalid ID",
"EK_L_C01": "Impossible de reserver une lesson : nombre maximal de lesson attein {{dateLesson}} "
},
I have my message displayed but there is no date value.
As you see I want to mention the date used as error message variable but I dont know how, so How can I add this date value to my error message ?