0

In a project which uses Java 1.8, lombok and Spring Boot 2.1.5.RELEASE, created a custom exception which is not showing up anywhere in my logs files or stdout.


pom.xml:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.5.RELEASE</version>
    <relativePath/>
</parent>

<dependencies>
   <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-web</artifactId>
   </dependency>
   <dependency>
       <groupId>org.projectlombok</groupId>
       <artifactId>lombok</artifactId>
       <optional>true</optional>
   </dependency>
</dependencies>

RestController:

@Slf4j
@RestController
@RequestMapping("/products")
public class RestController {

    private HttpHeaders headers = null;

    @Autowired
    ProductDao productDao;

    public RestController() {
        headers = new HttpHeaders();
        headers.add("Content-Type", "application/json");
    }

    @RequestMapping(value = {"/{id}"}, 
                    method = RequestMethod.GET, 
                    produces = "APPLICATION/JSON")
    public ResponseEntity<Object> getProductByUsingId(@PathVariable(value = "id", required = true) Integer id) 
    throws IOException, ProductNotFoundException {

        if (null == id) {
            return new ResponseEntity<Object>(HttpStatus.NOT_FOUND);
        }

        ProductResponse product = productDao.findProductById(id);

        if (null == product) {
            return new ResponseEntity<Object>(HttpStatus.NOT_FOUND);
        }
        return new ResponseEntity<Object>(product, headers, HttpStatus.OK);
    }
}

ProductNotFoundException:

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;

@ResponseStatus(value=HttpStatus.NOT_FOUND, reason="Product Not Found")
public class ProductNotFoundException extends Exception {

    private static final long serialVersionId = 1L;

    public ProductNotFoundException(int id) {
        super("Product does not exist with id: " + id);
    }
}

ControllerAdvice:

@Slf4j
@ControllerAdvice(annotations = RestController.class)
public class ProductControllerAdvice {

    @ExceptionHandler(ProductNotFoundException.class)
    @ResponseStatus(value= HttpStatus.NOT_FOUND)
    public void handleProductNotFoundException() {
        log.error("\n\n\tProductNotFoundException handler executed\n");
    }
}

When I hit my REST Endpoint:

http://localhost:8080/products/0

It returns an HTTP 404 - which it should.

But I don't see the ERROR anywhere in my logs.log (using grep -rnw logs.log -e 'ERROR') file and/or inside stdout?

Also, tried having ProductNotException extend RuntimeException, and it still didn't work...

What am I possibly doing wrong?

PacificNW_Lover
  • 4,746
  • 31
  • 90
  • 144
  • 1
    the controller doesn't seem to throw an exception.. – stringy05 Nov 14 '19 at 23:41
  • @stringy05 - You don’t see where I placed ```throws IOException, ProductNotFoundException``` after the method signature of ```getProductById()```? – PacificNW_Lover Nov 14 '19 at 23:53
  • This may not be helpful, but it works for me using your classes unchanged. I needed to add *-A 5* to the grep command to output the message after the error. – DanArl Nov 15 '19 at 00:03
  • 2
    `throws` just tells the compiler the method throws that sort of exception, the code needs to actually `throw` it for the exception handler to be triggered. instead of `return new ResponseEntity(HttpStatus.NOT_FOUND);` you could use `throw new ProductNotFoundException();`, that would trigger the exception handler – stringy05 Nov 15 '19 at 00:06

0 Answers0