您最喜欢的 Grails 调试技巧是什么? [英] What are your favorite Grails debugging tricks?

查看:21
本文介绍了您最喜欢的 Grails 调试技巧是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Grails 的长堆栈转储可能有点难以调试.找到问题的根源可能很棘手.例如,我在 BootStrap.groovy 中执行def foo = new Foo(a: a, b: b).save()"时被烧了几次.您最喜欢的调试 Grails 应用程序的技巧是什么?

Grails can be a bit of a bear to debug with its long stack dumps. Getting to the source of the problem can be tricky. I've gotten burned a few times in the BootStrap.groovy doing "def foo = new Foo(a: a, b: b).save()", for example. What are your favorite tricks for debugging Grails apps?

推荐答案

一些常规提示:

  • 清除 stacktrace.log,执行 grails run-app,然后在查看器中打开 stacktrace.log(我更喜欢 Linux 上的 less stacktrace.log)...一旦在你的查看器中,搜索 .groovy 和 .gsp...通常会让您了解您真正关心的内容.
  • 当堆栈跟踪引用 GSP 文件中的行号时,您应该在查询字符串中使用 ?showSource 在浏览器中打开该视图,即 http://localhost:8080/myProject/myController/myAction?showSource... 这里显示的是编译后的 GSP 源代码,stacktrace 中的所有 GSP 行号都是指编译后的 GSP,而不是实际的 GSP 源代码
  • 始终,始终,始终围绕您的保存至少进行一些最小的错误处理.
  • Clear stacktrace.log, do grails run-app, then open stacktrace.log in a viewer (I prefer less stacktrace.log on linux)... once in your viewer, search for .groovy and .gsp... that generally brings you to what you actually care about.
  • When a stacktrace refers to a line number in a GSP file, you should open that view in a browser with ?showSource in the query string, i.e. http://localhost:8080/myProject/myController/myAction?showSource... this shows the compiled GSP source, and all GSP line numbers in the stacktrace refer to the compiled GSP, not the actual GSP source
  • Always, always, always surround your saves with at least some minimal error handling.

示例:

try {
    if(!someDomainObject.save()) {
        throw new Exception ("Save failed")
    } 
} catch(Exception e) {
    println e.toString()
    // This will at least tell you what is wrong with
    // the instance you are trying to save
    someDomainObject.errors.allErrors.each {error ->
        println error.toString()
    }
}

除此之外,很多都归结为识别堆栈跟踪和错误消息……很多时候,Grails 提供给您的错误消息非常无用,但您可以学习识别模式,例如以下:

Beyond that, a lot of it just comes down to recognizing stacktraces and error messages... a lot of the time, Grails is incredibly unhelpful in the error messages it gives you, but you can learn to recognize patterns, like the following:

  • 一些最难理解的错误是因为您没有运行 grails cleangrails upgrade... 为避免这些问题,我总是使用在命令行上执行以下命令来运行 grails: grails clean;是 |圣杯升级;grails 运行应用程序
  • 如果错误与类的重复定义有关,请确保在类文件的顶部声明类所属的包
  • 如果错误与架构元数据、连接、套接字或任何类似的东西有关,请确保您的数据库连接器位于 lib/ 中,确保您的权限在 中都正确DataSource.groovy 和数据库中的用户名、密码和主机,并确保您知道连接器版本的来龙去脉(即 mysql 连接器版本 5.1.X 有一个奇怪的别名问题,可能需要您在 DataSource.groovy)
  • 中的 url 上设置 useOldAliasMetadataBehavior=true
  • Some of the hardest errors to make sense of are because you didn't run grails clean or grails upgrade... to avoid these problems, I always use the following on the command line to run grails: grails clean; yes | grails upgrade; grails run-app
  • If the error has to do with duplicate definitions of a class, make sure that you declare the package the class belongs to at the top of the class's file
  • If the error has to do with schema metadata, connection, socket, or anything like that, make sure your database connector is in lib/, make sure your permissions are correct both in DataSource.groovy and in the database for username, password, and host, and make sure that you know the ins and outs of your connector's version (i.e. mysql connector version 5.1.X has a weird issue with aliases that may require you to set useOldAliasMetadataBehavior=true on the url in DataSource.groovy)

等等.有很多模式需要学习识别.

And so on. There are a lot of patterns to learn to recognize.

这篇关于您最喜欢的 Grails 调试技巧是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆