我有一个相当简单的 Grails 控制器操作,它将参数绑定到域实例并将其传递给处理持久性的服务。
def finishBooking() {
Booking booking = new Booking(params)
try {
booking = bookingService.saveBooking(booking)
} catch (ReservationException e) {
log.error("Error saving booking", e)
flash.message = "Couldn't save the reservation."
render(view: "index", model: [booking: booking])
return
}
if (booking.hasErrors()) {
flash.message = "Reservation failed. Check the required fields."
render(view: "index", model: [booking: booking])
} else {
[booking: booking]
}
}
根据codenarc,catch 块中的 return 语句是一种不好的做法。您将如何实现错误处理?