Skip to content

Task 2-2: Improve Error Message cho lang_key Requirement

Phase: 2 - Validation Priority: Medium Module: webmarketplace Depends on: Không có Reference: docs/BountyHunter-Backend/details/feature-nft-management/SPEC.md

Background

GET /api/nft-rental/my-bounty-balls yêu cầu lang_key param nhưng nếu thiếu, throw LANG_KEY_IS_INVALID. Client không biết lý do lỗi nếu không đọc kỹ error code.

Tasks

DI Note: NftRentalController inject service để xử lý logic — không cần thêm injection cho validation. Spring MVC tự inject HttpServletRequest/HttpServletResponse nếu cần. @RequestParam(required = true)@Parameter là annotation-based, không cần runtime injection.

File: webmarketplace/controllers/nft_rental/NftRentalController.java

  • [ ] Thêm explicit validation với message rõ ràng hơn:
    @GetMapping("/my-bounty-balls")
    public Result<PageModel<NftBountyBallDto>> getMyBountyBalls(
        @RequestParam(required = true) String langKey,
        // ... other params
    ) {
        if (langKey == null || langKey.isBlank()) {
            // Nếu Spring không tự throw, validate thủ công
            throw new BadRequestException("LANG_KEY_IS_REQUIRED",
                "lang_key parameter is required. Supported values: en, ja, vi");
        }
        // ...
    }
    
  • Lưu ý: @RequestParam(required = true) là default — Spring tự throw MissingServletRequestParameterException nếu thiếu. Verify xem global exception handler có handle MissingServletRequestParameterException chưa (grep trong GlobalExceptionHandler hoặc @ControllerAdvice).

  • [ ] Document supported lang_key values trong Swagger @Parameter annotation:

    @Parameter(
        description = "Language key. Supported: en, ja, vi",
        required = true,
        schema = @Schema(allowableValues = {"en", "ja", "vi"})
    )
    @RequestParam String langKey
    

  • [ ] Nếu có GlobalExceptionHandler: đảm bảo MissingServletRequestParameterException trả về message user-friendly thay vì raw Spring error

Verification / Acceptance Criteria

  • [ ] GET /api/nft-rental/my-bounty-balls không có lang_key → trả 400 với message rõ ràng (không phải generic error)
  • [ ] Response body chứa thông tin: param bị thiếu là lang_key và supported values (en, ja, vi)
  • [ ] Swagger UI hiển thị lang_key là required với allowable values
  • [ ] GET /api/nft-rental/my-bounty-balls?lang_key=en hoạt động bình thường
  • [ ] GET /api/nft-rental/my-bounty-balls?lang_key=invalid trả error phù hợp (LANG_KEY_IS_INVALID)

Files to Modify

  • webmarketplace/controllers/nft_rental/NftRentalController.java