1

パラメータをドメイン インスタンスにバインドし、それを永続化を処理するサービスに渡す、かなり単純な 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 ステートメントは悪い習慣です。他にどのようにエラー処理を実装しますか?

4

2 に答える 2

2

+1 @Mr. ネコ。何かのようなもの。

def finishBooking() {
    Booking booking = new Booking(params)
    try {
        booking = bookingService.saveBooking(booking)
        if (booking.hasErrors()) {
            flash.message = "Reservation failed. Check the required fields."
            render(view: "index", model: [booking: booking])
        }
    } catch (ReservationException e) {
        log.error("Error saving booking", e)
        flash.message = "Couldn't save the reservation."
        render(view: "index", model: [booking: booking])
    }

    [booking: booking]
}
于 2013-05-23T15:29:57.973 に答える