title: How to customize the internal server error message (error code : 500)
body: <p>Consider a scenario where I want to create a record in database with the already existing ID. By doing this, we get &quot;500-internal server error&quot;. But I want to customize the message returned to &quot;Id already exist, Cannot create record&quot;. Below is the my sample existing code:
  Controller class :</p>
  <pre><code>    @PostMapping(value = &quot;/plans&quot;)
      public ResponseEntity&lt;ResponseSave&gt; savePostPlanDetails(
              @Valid @RequestBody(required = true) Plan plan) throws ParseException {
  
          String planId = plan.getPlanId();
  
          Integer createdId = planDataService.savePlan(plan, planId);
  
          ServiceMessage serviceMessage = ServiceMessage.createCreatedServiceMessage();
          return new ResponseEntity&lt;&gt;(ResponseSave.createResponseSave(serviceMessage, createdId), HttpStatus.CREATED);
      }
  </code></pre>
  <p>Service class :</p>
  <pre><code>public Integer savePlan(Plan plan, String planId) throws ParseException {
  
  
          PlanDao findResponse = planDataRepository.findByPlanId(planId);
  
          if (findResponse != null) {
              //This line generate 500 error if create with already existing ID.
              throw new IllegalArgumentException(PlanSearchEnum.RECORD_ALREADY_EXIST.getValue());                                                                                                 
          }
          
          PlanDao planDao1 = requestToResponseMapper.panToPlanDao(plan);
          PlanDao saveResponse = planDataRepository.save(planDao1);
          return saveResponse.getInternalId();
  
      }
  </code></pre>
  <p>Postman Output :</p>
  <pre><code>{
      &quot;message&quot;: {
          &quot;code&quot;: &quot;500&quot;,
          &quot;description&quot;: &quot;Unable to process request&quot;,
          &quot;type&quot;: &quot;Internal Server Error&quot;
      }
  }
  </code></pre>
  <p>As in the above postman response, I want the description to be like : &quot;description&quot;: &quot;Id already exist, Cannot create record&quot; instead of the general message as show above. So how to do this ?</p>