Git Product home page Git Product logo

springboot-globalexceptionhandling's Introduction

SpringBoot-GlobalExceptionHandling

https://nitinkc.github.io/spring/microservices/spring-exceptions/

curl --location 'localhost:8089/api/word' \
--header 'Content-Type: application/json' \
--data '{
    "word":"discover",
    "max":"5"
}'

Invoke the number format exception

curl --location 'localhost:8089/api/word' \
--header 'Content-Type: application/json' \
--data '{
    "word":"discover",
    "max":"word"
}'

Validation Exceptions

For the Request body

@Data
public class MyRequestDTO {
    @NotNull
    private String max;

    @NotNull
    @Size(min = 3, max = 10)
    private String word;
}
@PostMapping(path = "/word")
    public ResponseEntity<List<String>> runController(@RequestBody @Valid MyRequestDTO request) {
}

MethodArgumentNotValidException from the Jakarta Validation

  • ConstraintViolationException
  • MethodArgumentNotValidException
curl --location 'localhost:8089/api/word' \
--header 'Content-Type: application/json' \
--data '{
    "word":"tt",
    "max":"five"
}'
{
    "from": "Exception Response :: MethodArgumentNotValidException",
    "errorCode": "140 :: Error: :: Validation Error",
    "errorMessage": "{word=size must be between 3 and 10}",
    "methodName": "POST",
    "requestedURI": "/api/word",
    "thrownByMethod": "resolveArgument",
    "thrownByClass": "org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor",
    "exceptionType": null,
    "timestamp": "2024-03-06 23:51:08.663 PM MST(GMT-7)"
}

BadInputException

if(!StringUtils.isNumeric(max)){
            throw new BadInputException("String is not a Number");
        }
curl --location 'localhost:8089/api/word' \
--header 'Content-Type: application/json' \
--data '{
    "word":"12345",
    "max":"five"
}'
{
    "from": "From Exception Response :: handleInputExceptions",
    "errorCode": "140 :: Error: :: Validation Error",
    "errorMessage": "String is not a Number",
    "methodName": "POST",
    "requestedURI": "/api/word",
    "thrownByMethod": "runController",
    "thrownByClass": "com.spring.GlobalExceptionHandling.controller.SimpleController",
    "exceptionType": "BadInputException",
    "timestamp": "2024-03-06 23:55:18.036 PM MST(GMT-7)"
}

When there is data, either an empty response can be sent or no data found exception can be thrown based on business use case

List<String> data = service.getData(request.getWord(), max);
    if (data.isEmpty()){
        throw new WordsNotFoundException("No Words found");
    }
curl --location 'localhost:8089/api/word' \
--header 'Content-Type: application/json' \
--data '{
    "word":"12345",
    "max":"5"
}'
{
    "from": "From Traceable Error",
    "path": "/api/word",
    "timestamp": "2024-03-07 00:01:50.683 AM MST(GMT-7)",
    "errorCode": "123",
    "errorDescription": "ERROR: DB is down",
    "exceptionType": "WordsNotFoundException",
    "statusCode": "UNAVAILABLE_FOR_LEGAL_REASONS",
    "thrownByMethod": "runController",
    "thrownByClass": "com.spring.GlobalExceptionHandling.controller.SimpleController",
    "exceptionMessage": "No Words found",
    "errorTitle": "124 :: Error: :: Some Business Exception"
}

Validator

@Documented
@Constraint(validatedBy = EnglishWordValidator.class)
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface EnglishWord {
    String message() default "Word must be a valid English word";
    Class<?>[] groups() default {};
    Class<? extends Payload>[] payload() default {};
}

EnglishWordValidator class implementation

public class EnglishWordValidator implements ConstraintValidator<EnglishWord, String> {

    @Override
    public void initialize(EnglishWord constraintAnnotation) {
    }

    @Override
    public boolean isValid(String word, ConstraintValidatorContext constraintValidatorContext) {
        // implement logic here to check if the word is valid.
        // This may involve calling an external service or using an external library.

        return checkIfWordIsValidDictionaryWord(word);

    }

    private boolean checkIfWordIsValidDictionaryWord(String word) {
        // For demonstration purposes, all alphanumeric words are non dictionary words
        return !StringUtils.isAlphanumeric(word);
    }
}

The custom validator can be used as below :-

@Data
public class MyRequestDTO {
    @NotNull
    private String max;

    @NotNull
    @Size(min = 3, max = 10)
    @EnglishWord
    private String word;
}

springboot-globalexceptionhandling's People

Contributors

nitinkc avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.