gpt4 book ai didi

scala - Salat:如何调试堆栈跟踪以了解案例类中的哪个字段导致异常

转载 作者:行者123 更新时间:2023-12-02 01:54:25 26 4
gpt4 key购买 nike

我在我的 Scala/Scalatra 项目中使用 Salat 来序列化 MongoDB 对象。这是一个普遍的问题,因为我试图从我的数据库中提取一个复杂的 MongoDB 对象,该对象嵌入了我为其创建案例类的不同哈希和数组。我可以逐个字段注释掉并找出案例类中的哪些内容被破坏,但这只是一种荒谬的调试方式。我得到的堆栈跟踪如下所示:

DefaultArg.safeValue$lzycompute(Grater.scala:406)
com.novus.salat.DefaultArg.safeValue(Grater.scala:402)
com.novus.salat.ConcreteGrater.safeDefault(Grater.scala:363)
com.novus.salat.ConcreteGrater$$anonfun$5$$anonfun$apply$3.apply(Grater.scala:262)
com.novus.salat.ConcreteGrater$$anonfun$5$$anonfun$apply$3.apply(Grater.scala:262)
scala.Option.orElse(Option.scala:257)
com.novus.salat.ConcreteGrater$$anonfun$5.apply(Grater.scala:262)
com.novus.salat.ConcreteGrater$$anonfun$5.apply(Grater.scala:247)
scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:244)
scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:244)
scala.collection.immutable.List.foreach(List.scala:318)
scala.collection.TraversableLike$class.map(TraversableLike.scala:244)
scala.collection.AbstractTraversable.map(Traversable.scala:105)
com.novus.salat.ConcreteGrater.asObject(Grater.scala:247)
com.novus.salat.transformers.in.DBObjectToInContext$class.transform0(Injectors.scala:333)
com.novus.salat.transformers.in.DBObjectToInContext$class.transform(Injectors.scala:339)
com.novus.salat.transformers.in.package$$anon$1.transform(Injectors.scala:66)
com.novus.salat.transformers.Transformer$$anonfun$1.apply(Transformer.scala:85)
com.novus.salat.transformers.Transformer$$anonfun$1.apply(Transformer.scala:85)
scala.Option.flatMap(Option.scala:170)
com.novus.salat.transformers.Transformer.transform_$bang(Transformer.scala:85)
com.novus.salat.Field.in_$bang(Field.scala:64)
com.novus.salat.ConcreteGrater$$anonfun$5$$anonfun$apply$2.apply(Grater.scala:262)
com.novus.salat.ConcreteGrater$$anonfun$5$$anonfun$apply$2.apply(Grater.scala:262)
scala.Option.flatMap(Option.scala:170)
com.novus.salat.ConcreteGrater$$anonfun$5.apply(Grater.scala:262)
com.novus.salat.ConcreteGrater$$anonfun$5.apply(Grater.scala:247)
scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:244)
scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:244)
scala.collection.immutable.List.foreach(List.scala:318)
scala.collection.TraversableLike$class.map(TraversableLike.scala:244)
scala.collection.AbstractTraversable.map(Traversable.scala:105)
com.novus.salat.ConcreteGrater.asObject(Grater.scala:247)
com.novus.salat.transformers.in.DBObjectToInContext$class.transform0(Injectors.scala:333)
com.novus.salat.transformers.in.DBObjectToInContext$class.transform(Injectors.scala:338)
com.novus.salat.transformers.in.package$$anon$2.com$novus$salat$transformers$in$TraversableInjector$$super$transform(Injectors.scala:123)
com.novus.salat.transformers.in.TraversableInjector$class.transformElement(Injectors.scala:370)
com.novus.salat.transformers.in.package$$anon$2.transformElement(Injectors.scala:123)
com.novus.salat.transformers.in.TraversableInjector$$anonfun$after$1.apply(Injectors.scala:385)
scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:244)
scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:244)
scala.collection.immutable.List.foreach(List.scala:318)
scala.collection.TraversableLike$class.map(TraversableLike.scala:244)
scala.collection.AbstractTraversable.map(Traversable.scala:105)
com.novus.salat.transformers.in.TraversableInjector$class.after(Injectors.scala:385)
com.novus.salat.transformers.in.package$$anon$2.after(Injectors.scala:123)
com.novus.salat.transformers.Transformer$$anonfun$1.apply(Transformer.scala:85)
com.novus.salat.transformers.Transformer$$anonfun$1.apply(Transformer.scala:85)
scala.Option.flatMap(Option.scala:170)
com.novus.salat.transformers.Transformer.transform_$bang(Transformer.scala:85)
com.novus.salat.Field.in_$bang(Field.scala:64)
com.novus.salat.ConcreteGrater$$anonfun$5$$anonfun$apply$2.apply(Grater.scala:262)
com.novus.salat.ConcreteGrater$$anonfun$5$$anonfun$apply$2.apply(Grater.scala:262)
scala.Option.flatMap(Option.scala:170)
com.novus.salat.ConcreteGrater$$anonfun$5.apply(Grater.scala:262)
com.novus.salat.ConcreteGrater$$anonfun$5.apply(Grater.scala:247)
scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:244)
scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:244)
scala.collection.immutable.List.foreach(List.scala:318)
scala.collection.TraversableLike$class.map(TraversableLike.scala:244)
scala.collection.AbstractTraversable.map(Traversable.scala:105)
com.novus.salat.ConcreteGrater.asObject(Grater.scala:247)
com.sportsnetwork.api.models.MLBPlayByPlay$$anonfun$findByGameId$1.apply(MLBPlayByPlay.scala:38)
com.sportsnetwork.api.models.MLBPlayByPlay$$anonfun$findByGameId$1.apply(MLBPlayByPlay.scala:38)
scala.Option.map(Option.scala:145)
com.sportsnetwork.api.models.MLBPlayByPlay$.findByGameId(MLBPlayByPlay.scala:38)
com.sportsnetwork.api.MLBController$$anonfun$3.apply(MLBController.scala:49)
org.scalatra.ScalatraBase$class.org$scalatra$ScalatraBase$$liftAction(ScalatraBase.scala:250)
org.scalatra.ScalatraBase$$anonfun$invoke$1.apply(ScalatraBase.scala:245)
org.scalatra.ScalatraBase$$anonfun$invoke$1.apply(ScalatraBase.scala:245)
org.scalatra.ApiFormats$class.withRouteMultiParams(ApiFormats.scala:175)
com.sportsnetwork.api.MLBController.withRouteMultiParams(MLBController.scala:20)
org.scalatra.ScalatraBase$class.invoke(ScalatraBase.scala:244)
com.sportsnetwork.api.MLBController.org$scalatra$json$JsonSupport$$super$invoke(MLBController.scala:20)
org.scalatra.json.JsonSupport$$anonfun$invoke$1.apply(JsonSupport.scala:65)
org.scalatra.json.JsonSupport$$anonfun$invoke$1.apply(JsonSupport.scala:57)
org.scalatra.ApiFormats$class.withRouteMultiParams(ApiFormats.scala:175)
com.sportsnetwork.api.MLBController.withRouteMultiParams(MLBController.scala:20)
org.scalatra.json.JsonSupport$class.invoke(JsonSupport.scala:57)
com.sportsnetwork.api.MLBController.invoke(MLBController.scala:20)
org.scalatra.ScalatraBase$$anonfun$runRoutes$1$$anonfun$apply$7.apply(ScalatraBase.scala:220)
org.scalatra.ScalatraBase$$anonfun$runRoutes$1$$anonfun$apply$7.apply(ScalatraBase.scala:218)
scala.Option.flatMap(Option.scala:170)
org.scalatra.ScalatraBase$$anonfun$runRoutes$1.apply(ScalatraBase.scala:218)
org.scalatra.ScalatraBase$$anonfun$runRoutes$1.apply(ScalatraBase.scala:217)
scala.collection.immutable.Stream.flatMap(Stream.scala:442)
org.scalatra.ScalatraBase$class.runRoutes(ScalatraBase.scala:217)
org.scalatra.ScalatraServlet.runRoutes(ScalatraServlet.scala:49)
org.scalatra.ScalatraBase$class.runActions$1(ScalatraBase.scala:163)
org.scalatra.ScalatraBase$$anonfun$executeRoutes$1.apply$mcV$sp(ScalatraBase.scala:175)
org.scalatra.ScalatraBase$$anonfun$executeRoutes$1.apply(ScalatraBase.scala:175)
org.scalatra.ScalatraBase$$anonfun$executeRoutes$1.apply(ScalatraBase.scala:175)
org.scalatra.ScalatraBase$class.org$scalatra$ScalatraBase$$cradleHalt(ScalatraBase.scala:190)
org.scalatra.ScalatraBase$class.executeRoutes(ScalatraBase.scala:175)
org.scalatra.ScalatraServlet.executeRoutes(ScalatraServlet.scala:49)
org.scalatra.ScalatraBase$$anonfun$handle$1.apply$mcV$sp(ScalatraBase.scala:113)
org.scalatra.ScalatraBase$$anonfun$handle$1.apply(ScalatraBase.scala:113)
org.scalatra.ScalatraBase$$anonfun$handle$1.apply(ScalatraBase.scala:113)
scala.util.DynamicVariable.withValue(DynamicVariable.scala:57)
org.scalatra.DynamicScope$class.withResponse(DynamicScope.scala:80)
org.scalatra.ScalatraServlet.withResponse(ScalatraServlet.scala:49)
org.scalatra.DynamicScope$$anonfun$withRequestResponse$1.apply(DynamicScope.scala:60)
scala.util.DynamicVariable.withValue(DynamicVariable.scala:57)
org.scalatra.DynamicScope$class.withRequest(DynamicScope.scala:71)
org.scalatra.ScalatraServlet.withRequest(ScalatraServlet.scala:49)
org.scalatra.DynamicScope$class.withRequestResponse(DynamicScope.scala:59)
org.scalatra.ScalatraServlet.withRequestResponse(ScalatraServlet.scala:49)
org.scalatra.ScalatraBase$class.handle(ScalatraBase.scala:111)
org.scalatra.ScalatraServlet.org$scalatra$servlet$ServletBase$$super$handle(ScalatraServlet.scala:49)
org.scalatra.servlet.ServletBase$class.handle(ServletBase.scala:43)
com.sportsnetwork.api.MLBController.org$scalatra$scalate$ScalateSupport$$super$handle(MLBController.scala:20)
org.scalatra.scalate.ScalateSupport$class.handle(ScalateSupport.scala:122)
com.sportsnetwork.api.MLBController.org$scalatra$CorsSupport$$super$handle(MLBController.scala:20)
org.scalatra.CorsSupport$$anonfun$handle$1.apply$mcV$sp(CORSSupport.scala:202)
org.scalatra.CorsSupport$$anonfun$handle$1.apply(CORSSupport.scala:190)
org.scalatra.CorsSupport$$anonfun$handle$1.apply(CORSSupport.scala:190)
scala.util.DynamicVariable.withValue(DynamicVariable.scala:57)
org.scalatra.DynamicScope$class.withResponse(DynamicScope.scala:80)
org.scalatra.ScalatraServlet.withResponse(ScalatraServlet.scala:49)
org.scalatra.DynamicScope$$anonfun$withRequestResponse$1.apply(DynamicScope.scala:60)
scala.util.DynamicVariable.withValue(DynamicVariable.scala:57)
org.scalatra.DynamicScope$class.withRequest(DynamicScope.scala:71)
org.scalatra.ScalatraServlet.withRequest(ScalatraServlet.scala:49)
org.scalatra.DynamicScope$class.withRequestResponse(DynamicScope.scala:59)
org.scalatra.ScalatraServlet.withRequestResponse(ScalatraServlet.scala:49)
org.scalatra.CorsSupport$class.handle(CORSSupport.scala:188)
com.sportsnetwork.api.MLBController.handle(MLBController.scala:20)
org.scalatra.ScalatraServlet.service(ScalatraServlet.scala:54)
javax.servlet.http.HttpServlet.service(HttpServlet.java:848)
org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:669)
org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:455)
org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:137)
org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:560)
org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:231)
org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1072)
org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:382)
org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:193)
org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1006)
org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:135)
org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:255)
org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:116)
org.eclipse.jetty.server.Server.handle(Server.java:361)
org.eclipse.jetty.server.AbstractHttpConnection.handleRequest(AbstractHttpConnection.java:485)
org.eclipse.jetty.server.AbstractHttpConnection.headerComplete(AbstractHttpConnection.java:926)
org.eclipse.jetty.server.AbstractHttpConnection$RequestHandler.headerComplete(AbstractHttpConnection.java:988)
org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:635)
org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:235)
org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:82)
org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:628)
org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:52)
org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:608)
org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:543)
java.lang.Thread.run(Thread.java:701)

我的案例类的一个例子如下:

case class MLBPlayByPlay(datetime: DateTime, gamedate: DateTime, game_id: Int, home_team_id: Int, away_team_id: Int,
home_team_score: Int, away_team_score: Int, sport: String, season_year: Int, season_type: String, location: String, status: String,
home_team_abbr: String, away_team_abbr: String, hp_umpire: String, fb_umpire: String, sb_umpire: String, tb_umpire: String,
play_by_play_seqs: Seq[MLBPlayByPlaySeq], created_at: Option[Date] = None, updated_at: Date)

case class MLBPlayByPlaySeq(batter: Option[MLBBatter] = None, next_batter: Option[MLBBatter] = None, pitchers: Option[Seq[MLBPitcher]] = None, pitches: Option[Seq[MLBPitches]] = None, fielders: Option[Seq[MLBFielder]] = None,
seq_id: Int, inning: Option[Int] = None, outs: Option[Int] = None, no_play: Option[Int] = None, home_team_score: Int, away_team_score: Int)

case class MLBBatter(batter_id: Int, team_id: Int, first_name: String, last_name: String, action: Option[String] = None, out: Option[Int] = None, adv: Option[Int] = None,
to_base: Option[Int] = None, scored: Option[Int] = None, bats: Option[String] = None, appear: Option[Int] = None, pos: Option[String] = None,
pitcher_id: Option[Int] = None, pthrows: Option[String] = None, error_ids: Option[String] = None, location: Option[String] = None, traj: Option[String] = None,
field: Option[String] = None, coords: Option[String] = None, ab: Option[Int] = None, gndout: Option[Int] = None)

case class MLBPitcher(pitcher_id: Int, team_id: Int, first_name: String, last_name: String, inning: Int, outs: Int, throws: String, bbats: String, ab: Int, bf: Int)
case class MLBFielder(fielder_id: Int, team_id: Int, first_name: String, last_name: String, pos: String, assist: Int, put_out: Int)
case class MLBPitches(count: Int, total_pitches: Int, balls: Int, strikes: Int, value: String, batter_id: Int, pitcher_id: Int, runner_1_id: Int, runner_2_id: Int,
runner_3_id: Int, final_pitch: String)

我从 MLBPlayByPlay 案例类开始,通过逐个字段注释掉它来开始调试,并让 API 响应良好。最后,当我取消注释 play_by_play_seqs: Seq[MLBPlayByPlaySeq] 时,一切都崩溃了。现在我必须进入 MLBPlayByPlaySeq 案例类,它嵌入了自己的案例类类型,并开始在那里一个一个地注释掉……肯定有更简单的调试 Salat 的方法吗?这就是我使用刨丝器的方式:

 val res = MLBPlayByPlayDAO.findByGameId(gameId)
res.map(grater[MLBPlayByPlay].asObject(_))

实际异常:

k.api.MLBController - exception occured: com.novus.salat.DefaultArg.safeValue$lzycompute(Grater.scala:406)

将在我从字段中删除 Option[Seq[T]] 后不久更新线程。

** 更新 2 **

从我的案例类中删除所有 Option[Seq[T]] 后,我将问题缩小到当 next_batter 在我的数据库中是一个空哈希 MLBPlayByPlaySeqs 案例类。我将定义从 next_batter: Option[MLBBatter] = None(这会导致它在空散列上爆炸)更改为 next_batter: Map[String, Any] = Map.empty 并且有效。这里的问题是:

  • 我使用 map 失去了类型安全
  • 我无法再手动选择要在序列化 JSON 输出中列出的属性。嵌入式哈希的 created_at 和 updated_at 之类的东西出现在我的 JSON 中,这些是我在案例类中明确遗漏的东西,我无法通过将 next_batter 更改为 Map 与案例类来做到这一点。

最佳答案

在此向作者致敬 - 你能分享堆栈跟踪中的实际异常吗?

Salat 不支持包含集合的选项 - 我看到您在 MLBPlayByPlaySeq 中有一些选项。尝试将它们转换为默认值为 Seq.emptySeq,看看是否可以解决问题。

如果没有,请发布实际异常,我会再看一遍。

关于scala - Salat:如何调试堆栈跟踪以了解案例类中的哪个字段导致异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21009170/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com