0
votes

I'm having the slight issue that spring boot is providing me with absolutely convoluted error messages, which none of my customers can or will understand.

for example:

this is my property configuration class

@Component
@ConfigurationProperties(prefix = "input")
class WorkflowRunnerProperties{

  @Valid
  @NotNull(message = "please provide a file containing your targets for the parameter --input.libraryTargets")
  val libraryTargets:File = null

  @Valid
  @NotNull(message = "please provide a file containing your targets for the parameter --input.correctionTargets")
  val correctionTargets:File = null
}

And during start up I receive this error message:

ERROR [main] SpringApplication - Application startup failed org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'workflowRunnerProperties': Could not bind properties to WorkflowRunnerProperties (prefix=input, ignoreInvalidFields=false, ignoreUnknownFields=true, ignoreNestedProperties=false); nested exception is org.springframework.validation.BindException: org.springframework.boot.bind.RelaxedDataBinder$RelaxedBeanPropertyBindingResult: 2 errors Field error in object 'input' on field 'correctionTargets': rejected value [null]; codes [NotNull.input.correctionTargets,NotNull.correctionTargets,NotNull]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [input.correctionTargets,correctionTargets]; arguments []; default message [correctionTargets]]; default message [please provide a file containing your targets for the parameter --input.correctionTargets] Field error in object 'input' on field 'libraryTargets': rejected value [null]; codes [NotNull.input.libraryTargets,NotNull.libraryTargets,NotNull]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [input.libraryTargets,libraryTargets]; arguments []; default message [libraryTargets]]; default message [please provide a file containing your targets for the parameter --input.libraryTargets] at org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor.postProcessBeforeInitialization(ConfigurationPropertiesBindingPostProcessor.java:339) at org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor.postProcessBeforeInitialization(ConfigurationPropertiesBindingPostProcessor.java:289) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:408) ...

which is nice, but provides so many information that it defeats the purpose, instead all I want to see is:

please provide a file containing your targets for the parameter --input.correctionTargets please provide a file containing your targets for the parameter --input.libraryTargets

what would be the easiest way to catch this exception and only log the actual relevant information for the user, without overwhelming him with all the developer stuff.

thanks

1
The app's broken (from what I can see of your stack), so the error message you're trying to isolate wouldn't occur. - Makoto
thank you but how is this answer related? I was asking for getting direct access to the exception and not receiving the whole spring stacktrace, so I can provide the user with a decent understandable error message instead of the whole spring stacktrace. - berlinguyinca

1 Answers

0
votes

best approach I found so far, is catching all the related exceptions and logging it out to the console like this:

object SimpleTargetRunner extends App with LazyLogging {
  try {
    val app = new SpringApplication(classOf[SimpleTargetRunner])
    app.setWebEnvironment(false)
    val context = app.run(args: _*)
  }
  catch {
    case x: BeanCreationException =>
      x.getMostSpecificCause match {
        case y:BindException =>
          logger.info("dear user, we need you to provide a couple of parameters.     These are the errors we observed:")
          y.getAllErrors.asScala.foreach{ error:ObjectError =>
            logger.info(s"${error.getDefaultMessage}")
          }
      }
  }
}

which results in a logging statement like this:

dear user, we need you to provide a couple of parameters. These are the errors we observed: please provide a file containing your targets for the parameter --input.libraryTargets please provide a file containing your targets for the parameter --input.correctionTargets