@@ -380,7 +380,8 @@ protected ResponseEntity<Object> handleErrorResponseException(
380
380
/**
381
381
* Customize the handling of {@link ConversionNotSupportedException}.
382
382
* <p>By default this method creates a {@link ProblemDetail} with the status
383
- * and a short detail message, and then delegates to
383
+ * and a short detail message, and also looks up an override for the detail
384
+ * via {@link MessageSource}, before delegating to
384
385
* {@link #handleExceptionInternal}.
385
386
* @param ex the exception to handle
386
387
* @param headers the headers to use for the response
@@ -393,16 +394,19 @@ protected ResponseEntity<Object> handleErrorResponseException(
393
394
protected ResponseEntity <Object > handleConversionNotSupported (
394
395
ConversionNotSupportedException ex , HttpHeaders headers , HttpStatusCode status , WebRequest request ) {
395
396
396
- ProblemDetail body = ProblemDetail .forStatusAndDetail (status ,
397
- "Failed to convert '" + ex .getPropertyName () + "' with value: '" + ex .getValue () + "'" );
397
+ Object [] args = {ex .getPropertyName (), ex .getValue ()};
398
+
399
+ ProblemDetail body = resolveDetailViaMessageSource (
400
+ status , args , "Failed to convert '" + args [0 ] + "' with value: '" + args [1 ] + "'" );
398
401
399
402
return handleExceptionInternal (ex , body , headers , status , request );
400
403
}
401
404
402
405
/**
403
406
* Customize the handling of {@link TypeMismatchException}.
404
407
* <p>By default this method creates a {@link ProblemDetail} with the status
405
- * and a short detail message, and then delegates to
408
+ * and a short detail message, and also looks up an override for the detail
409
+ * via {@link MessageSource}, before delegating to
406
410
* {@link #handleExceptionInternal}.
407
411
* @param ex the exception to handle
408
412
* @param headers the headers to use for the response
@@ -415,16 +419,19 @@ protected ResponseEntity<Object> handleConversionNotSupported(
415
419
protected ResponseEntity <Object > handleTypeMismatch (
416
420
TypeMismatchException ex , HttpHeaders headers , HttpStatusCode status , WebRequest request ) {
417
421
418
- ProblemDetail body = ProblemDetail .forStatusAndDetail (status ,
419
- "Unexpected type for '" + ex .getPropertyName () + "' with value: '" + ex .getValue () + "'" );
422
+ Object [] args = {ex .getPropertyName (), ex .getValue ()};
423
+
424
+ ProblemDetail body = resolveDetailViaMessageSource (
425
+ status , args , "Failed to convert '" + args [0 ] + "' with value: '" + args [1 ] + "'" );
420
426
421
427
return handleExceptionInternal (ex , body , headers , status , request );
422
428
}
423
429
424
430
/**
425
431
* Customize the handling of {@link HttpMessageNotReadableException}.
426
432
* <p>By default this method creates a {@link ProblemDetail} with the status
427
- * and a short detail message, and then delegates to
433
+ * and a short detail message, and also looks up an override for the detail
434
+ * via {@link MessageSource}, before delegating to
428
435
* {@link #handleExceptionInternal}.
429
436
* @param ex the exception to handle
430
437
* @param headers the headers to use for the response
@@ -437,14 +444,15 @@ protected ResponseEntity<Object> handleTypeMismatch(
437
444
protected ResponseEntity <Object > handleHttpMessageNotReadable (
438
445
HttpMessageNotReadableException ex , HttpHeaders headers , HttpStatusCode status , WebRequest request ) {
439
446
440
- ProblemDetail body = ProblemDetail . forStatusAndDetail (status , "Failed to read request body " );
447
+ ProblemDetail body = resolveDetailViaMessageSource (status , null , "Failed to read request" );
441
448
return handleExceptionInternal (ex , body , headers , status , request );
442
449
}
443
450
444
451
/**
445
452
* Customize the handling of {@link HttpMessageNotWritableException}.
446
453
* <p>By default this method creates a {@link ProblemDetail} with the status
447
- * and a short detail message, and then delegates to
454
+ * and a short detail message, and also looks up an override for the detail
455
+ * via {@link MessageSource}, before delegating to
448
456
* {@link #handleExceptionInternal}.
449
457
* @param ex the exception to handle
450
458
* @param headers the headers to use for the response
@@ -457,7 +465,7 @@ protected ResponseEntity<Object> handleHttpMessageNotReadable(
457
465
protected ResponseEntity <Object > handleHttpMessageNotWritable (
458
466
HttpMessageNotWritableException ex , HttpHeaders headers , HttpStatusCode status , WebRequest request ) {
459
467
460
- ProblemDetail body = ProblemDetail . forStatusAndDetail (status , "Failed to write response body " );
468
+ ProblemDetail body = resolveDetailViaMessageSource (status , null , "Failed to write request " );
461
469
return handleExceptionInternal (ex , body , headers , status , request );
462
470
}
463
471
@@ -528,6 +536,17 @@ protected ResponseEntity<Object> handleExceptionInternal(
528
536
return createResponseEntity (body , headers , statusCode , request );
529
537
}
530
538
539
+ // For non-Web exceptions
540
+ private ProblemDetail resolveDetailViaMessageSource (
541
+ HttpStatusCode status , @ Nullable Object [] arguments , String defaultDetail ) {
542
+
543
+ ProblemDetail body = ProblemDetail .forStatusAndDetail (status , defaultDetail );
544
+ ErrorResponseException errorResponseEx = new ErrorResponseException (status , body , null , null , arguments );
545
+ body = resolveDetailViaMessageSource (errorResponseEx );
546
+ return body ;
547
+ }
548
+
549
+ // For ErrorResponse exceptions
531
550
private ProblemDetail resolveDetailViaMessageSource (ErrorResponse response ) {
532
551
ProblemDetail body = response .getBody ();
533
552
if (this .messageSource != null ) {
0 commit comments