Skip to content

Instantly share code, notes, and snippets.

View Raouf25's full-sized avatar

ABDERRAOUF MAKHLOUF Raouf25

View GitHub Profile
<dependency>
<groupId>com.pivovarit</groupId>
<artifactId>throwing-function</artifactId>
<version>1.5.0</version>
</dependency>
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import java.util.Arrays;
import java.util.List;
@Getter
@Setter
@Slf4j
public class ResourceNotFoundException extends BackendException {
public ResourceNotFoundException(Integer id, String message) {
super(ErrorEnum.RESOURCE_NOT_FOUND, "%s id = %s not found", message, id);
}
}
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
@Getter
@RequiredArgsConstructor
public enum ErrorEnum {
CLIENT_UNAVAILABLE_ERROR("Service unavailable.", HttpStatus.BAD_REQUEST),
RESOURCE_NOT_FOUND("Resource not found.", HttpStatus.NOT_FOUND);
private final String message;
public class ClientUnavailableException extends BackendException {
public ClientUnavailableException(String message) {
super(ErrorEnum.CLIENT_UNAVAILABLE_ERROR, "%s is unavailable", message);
}
}
public Agreement getById(Integer id) throws BackendException {
try {
AgreementDto agreementDto = this.agreementClientService.get(id);
return mapper.map(agreementDto, Agreement.class);
} catch (ResourceAccessException e) {
throw new CatalogClientUnavailableException();
} catch (CatalogException e) {
throw new ResourceNotFoundException(id, "Agreement");
}
}
@ControllerAdvice
@CrossOrigin
public class ExceptionHandlerController {
@ExceptionHandler({ClientUnavailableException.class})
public ResponseEntity<ErrorAndCodeResponse> badRequest(BackendException e) {
ErrorAndCodeResponse error = new ErrorAndCodeResponse(e.getCode().name(), e.getCode().getMessage(), e.getAdditionalInfo());
return new ResponseEntity<>(error, HttpStatus.BAD_REQUEST);
}
List list = new ArrayList();
list.add(10);
list.add("10");
List<Integer> list = new ArrayList<Integer>();
list.add(10);
list.add("10");// compile-time error
List list = new ArrayList();
list.add("hello");
String s = (String) list.get(0);//typecasting