This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<dependency> | |
<groupId>com.pivovarit</groupId> | |
<artifactId>throwing-function</artifactId> | |
<version>1.5.0</version> | |
</dependency> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import lombok.Getter; | |
import lombok.Setter; | |
import lombok.extern.slf4j.Slf4j; | |
import java.util.Arrays; | |
import java.util.List; | |
@Getter | |
@Setter | |
@Slf4j |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ResourceNotFoundException extends BackendException { | |
public ResourceNotFoundException(Integer id, String message) { | |
super(ErrorEnum.RESOURCE_NOT_FOUND, "%s id = %s not found", message, id); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
List list = new ArrayList(); | |
list.add(10); | |
list.add("10"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
List<Integer> list = new ArrayList<Integer>(); | |
list.add(10); | |
list.add("10");// compile-time error |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
List list = new ArrayList(); | |
list.add("hello"); | |
String s = (String) list.get(0);//typecasting |