简体   繁体   中英

spring boot cli decent error message

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM