简体   繁体   English

Spring Boot CLI体面错误消息

[英]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. 我有一个小问题,就是Spring Boot向我提供了绝对复杂的错误消息,我的客户都无法理解或将不会理解。

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); 错误[主要] SpringApplication-应用程序启动失败org.springframework.beans.factory.BeanCreationException:创建名称为'workflowRunnerProperties'的bean时出错:无法将属性绑定到WorkflowRunner属性(前缀=输入,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]; 嵌套异常是org.springframework.validation.BindException:org.springframework.boot.bind.RelaxedDataBinder $ RelaxedBeanPropertyBindingResult:2错误字段“ correctionTargets”上的对象“ input”中的字段错误:拒绝值[null]; codes [NotNull.input.correctionTargets,NotNull.correctionTargets,NotNull]; 代码[NotNull.input.correctionTargets,NotNull.correctionTargets,NotNull]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [input.correctionTargets,correctionTargets]; 参数[org.springframework.context.support.DefaultMessageSourceResolvable:代码[input.correctionTargets,correctionTargets]; arguments []; 参数[]; default message [correctionTargets]]; 默认消息[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]; 默认消息[请提供一个包含参数--input.correctionTargets的目标的文件]域'libraryTargets'上的对象'input'中的域错误:拒绝值[null]; codes [NotNull.input.libraryTargets,NotNull.libraryTargets,NotNull]; 代码[NotNull.input.libraryTargets,NotNull.libraryTargets,NotNull]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [input.libraryTargets,libraryTargets]; 参数[org.springframework.context.support.DefaultMessageSourceResolvable:代码[input.libraryTargets,libraryTargets]; arguments []; 参数[]; default message [libraryTargets]]; 默认消息[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) ... 默认消息[请提供包含参数--input.libraryTargets的目标的文件],位于org.springframework.boot.context.properties,位于org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor.postProcessBeforeInitialization(ConfigurationPropertiesBindingPostProcessor.java:339)在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 请提供一个包含参数--input.correctionTargets目标的文件请提供一个包含参数--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 这些是我们观察到的错误:请提供一个包含参数--input.libraryTargets的目标的文件,请提供一个包含参数--input.correctionTargets的目标的文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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