Skip to content

Commit 3c713f3

Browse files
committed
Tratando exceções adequadamente
1 parent 28737ae commit 3c713f3

File tree

3 files changed

+49
-3
lines changed

3 files changed

+49
-3
lines changed

src/main/java/jdc/loja/resources/exceptions/ResourceExceptionHandler.java

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,13 @@
99
import org.springframework.web.bind.annotation.ControllerAdvice;
1010
import org.springframework.web.bind.annotation.ExceptionHandler;
1111

12+
import com.amazonaws.AmazonClientException;
13+
import com.amazonaws.AmazonServiceException;
14+
import com.amazonaws.services.s3.model.AmazonS3Exception;
15+
1216
import jdc.loja.services.exceptions.AuthorizationException;
1317
import jdc.loja.services.exceptions.DataIntegrityException;
18+
import jdc.loja.services.exceptions.FileException;
1419
import jdc.loja.services.exceptions.ObjectNotFoundException;
1520

1621
@ControllerAdvice
@@ -38,8 +43,33 @@ public ResponseEntity<StandardError> validation(MethodArgumentNotValidException
3843
}
3944

4045
@ExceptionHandler(AuthorizationException.class)
41-
public ResponseEntity<StandardError> objectNotFound(AuthorizationException e, HttpServletRequest req) {
46+
public ResponseEntity<StandardError> authorization(AuthorizationException e, HttpServletRequest req) {
4247
StandardError err = new StandardError(HttpStatus.FORBIDDEN.value(), e.getMessage(), System.currentTimeMillis());
4348
return ResponseEntity.status(HttpStatus.FORBIDDEN).body(err);
4449
}
50+
51+
@ExceptionHandler(FileException.class)
52+
public ResponseEntity<StandardError> file(FileException e, HttpServletRequest req) {
53+
StandardError err = new StandardError(HttpStatus.BAD_REQUEST.value(), e.getMessage(), System.currentTimeMillis());
54+
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(err);
55+
}
56+
57+
@ExceptionHandler(AmazonServiceException.class)
58+
public ResponseEntity<StandardError> amazonService(AmazonServiceException e, HttpServletRequest req) {
59+
HttpStatus code = HttpStatus.valueOf(e.getErrorCode());
60+
StandardError err = new StandardError(code.value(), e.getMessage(), System.currentTimeMillis());
61+
return ResponseEntity.status(code).body(err);
62+
}
63+
64+
@ExceptionHandler(AmazonClientException.class)
65+
public ResponseEntity<StandardError> amazonClient(AmazonClientException e, HttpServletRequest req) {
66+
StandardError err = new StandardError(HttpStatus.BAD_REQUEST.value(), e.getMessage(), System.currentTimeMillis());
67+
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(err);
68+
}
69+
70+
@ExceptionHandler(AmazonS3Exception.class)
71+
public ResponseEntity<StandardError> amazonS3(AmazonS3Exception e, HttpServletRequest req) {
72+
StandardError err = new StandardError(HttpStatus.BAD_REQUEST.value(), e.getMessage(), System.currentTimeMillis());
73+
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(err);
74+
}
4575
}

src/main/java/jdc/loja/services/S3Service.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import com.amazonaws.services.s3.AmazonS3;
1616
import com.amazonaws.services.s3.model.ObjectMetadata;
1717

18+
import jdc.loja.services.exceptions.FileException;
19+
1820
@Service
1921
public class S3Service {
2022

@@ -33,7 +35,7 @@ public URI uploadFile(MultipartFile multipartFile) {
3335
String contentType = multipartFile.getContentType();
3436
return uploadFile(is, filename, contentType);
3537
} catch (IOException e) {
36-
throw new RuntimeException("Erro de IO: " + e.getMessage());
38+
throw new FileException("Erro de IO: " + e.getMessage());
3739
}
3840
}
3941

@@ -46,7 +48,7 @@ public URI uploadFile(InputStream is, String filename, String contentType) {
4648
LOG.info("Upload finalizado");
4749
return s3client.getUrl(bucketName, filename).toURI();
4850
} catch (URISyntaxException e) {
49-
throw new RuntimeException("Erro ao converter URL para URI");
51+
throw new FileException("Erro ao converter URL para URI");
5052
}
5153
}
5254
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package jdc.loja.services.exceptions;
2+
3+
public class FileException extends RuntimeException {
4+
5+
private static final long serialVersionUID = 1L;
6+
7+
public FileException(String msg) {
8+
super(msg);
9+
}
10+
11+
public FileException(String msg, Throwable cause) {
12+
super(msg, cause);
13+
}
14+
}

0 commit comments

Comments
 (0)