Skip to content

Commit ac977c8

Browse files
committed
improve message bundle and add messageContainer and message for unified message around the app
1 parent 5ac9e4b commit ac977c8

File tree

16 files changed

+217
-44
lines changed

16 files changed

+217
-44
lines changed

src/main/java/ir/bigz/springbootreal/commons/util/Utils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public static List<Sort.Order> getSortOrderFromPagedQuery(PagedQuery pagedQuery,
121121
orders.add(order);
122122
}catch (Exception e){
123123
throw AppException.newInstance(
124-
HttpErrorCode.ERR_10705, String.format("field %s ordering is wrong", orderParam)
124+
HttpErrorCode.CREATE_QUERY_ERROR, String.format("field %s ordering is wrong", orderParam)
125125
);
126126
}
127127
}

src/main/java/ir/bigz/springbootreal/controller/AbstractController.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
11
package ir.bigz.springbootreal.controller;
22

3+
import ir.bigz.springbootreal.commons.util.Utils;
34
import ir.bigz.springbootreal.dto.PagedQuery;
5+
import ir.bigz.springbootreal.exception.ExceptionType;
6+
import ir.bigz.springbootreal.exception.HttpExceptionModel;
7+
import ir.bigz.springbootreal.messages.MessageContainer;
8+
import org.springframework.context.MessageSource;
9+
import org.springframework.http.HttpStatus;
10+
import org.springframework.http.MediaType;
11+
import org.springframework.http.ResponseEntity;
412
import org.springframework.web.context.request.RequestContextHolder;
513
import org.springframework.web.context.request.ServletRequestAttributes;
614

715
import javax.servlet.http.HttpServletRequest;
816
import java.util.HashMap;
17+
import java.util.Locale;
918
import java.util.Map;
19+
import java.util.Objects;
1020
import java.util.stream.Collectors;
1121

1222
public abstract class AbstractController {
@@ -42,4 +52,25 @@ protected Map<String, String> getUnlimitedSizeParam() {
4252
}
4353
};
4454
}
55+
56+
protected ResponseEntity<?> getSuccessMessage(MessageSource messageSource, MessageContainer messageContainer, Locale locale) {
57+
return ResponseEntity.status(HttpStatus.OK)
58+
.headers(httpHeaders -> httpHeaders.set("Content-Type", MediaType.APPLICATION_JSON_VALUE + "; charset=utf-8"))
59+
.body(messageSource.getMessage(messageContainer.getMessages().get(0).getMessageKey(),
60+
messageContainer.getMessages().get(0).getMessageParams(), Objects.nonNull(locale) ? locale : Locale.getDefault()));
61+
}
62+
63+
protected ResponseEntity<?> getErrorMessage(MessageSource messageSource, ExceptionType exceptionType, Object[] messageParams) {
64+
return ResponseEntity.status(exceptionType.getHttpStatus())
65+
.headers(httpHeaders -> httpHeaders.set("Content-Type", MediaType.APPLICATION_JSON_VALUE + "; charset=utf-8"))
66+
.body(getErrorModelResponse(exceptionType, messageSource, messageParams));
67+
}
68+
69+
private HttpExceptionModel getErrorModelResponse(ExceptionType exceptionType, MessageSource messageSource, Object... messageParams) {
70+
return HttpExceptionModel.builder()
71+
.errorCode(exceptionType.getErrorCode())
72+
.message(messageSource.getMessage(exceptionType.getReasonMessage(), messageParams, Locale.getDefault()))
73+
.timestamp(Utils.getTimestampNow().toString())
74+
.build();
75+
}
4576
}

src/main/java/ir/bigz/springbootreal/controller/SampleController.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package ir.bigz.springbootreal.controller;
22

33
import ir.bigz.springbootreal.dto.PageResult;
4+
import ir.bigz.springbootreal.messages.MessageContainer;
45
import ir.bigz.springbootreal.service.UserService;
56
import ir.bigz.springbootreal.viewmodel.UserModel;
67
import ir.bigz.springbootreal.viewmodel.search.UserSearchDto;
@@ -47,7 +48,9 @@ public ResponseEntity<?> getErrorMessage(
4748
public ResponseEntity<?> getLocaleMessage(
4849
@RequestHeader(name = "Accept-Language", required = false) final Locale locale,
4950
@RequestParam(name = "username", defaultValue = "Java Geek", required = false) final String username) {
50-
return ResponseEntity.ok(source.getMessage("welcome.message", new Object[]{username}, locale));
51+
MessageContainer messageContainer = MessageContainer.create();
52+
messageContainer.add("welcome.message", new Object[]{username});
53+
return getSuccessMessage(source, messageContainer, locale);
5154
}
5255

5356

src/main/java/ir/bigz/springbootreal/dal/DaoRepositoryImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ public PageResult<T> pageCreateQuery(String nativeQuery, PagedQuery pagedQuery,
173173
orderString.append(orderColumn + " " + order);
174174
}catch (Exception e){
175175
throw AppException.newInstance(
176-
HttpErrorCode.ERR_10705, String.format("field %s ordering is wrong", orderParam)
176+
HttpErrorCode.CREATE_QUERY_ERROR, String.format("field %s ordering is wrong", orderParam)
177177
);
178178
}
179179
}

src/main/java/ir/bigz/springbootreal/exception/AppException.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ static public AppException newInstance(HttpStatus status, String message, Throwa
2626
}
2727

2828
static public AppException newInstance(HttpErrorCode errorCode, String message, Throwable cause) {
29-
return new AppException(errorCode.getStatus(), message, cause, errorCode, null);
29+
return new AppException(errorCode.getHttpStatus(), message, cause, errorCode, null);
3030
}
3131

3232
static public AppException newInstance(HttpErrorCode errorCode, String detail) {
33-
return new AppException(errorCode.getStatus(), null, null, errorCode, detail);
33+
return new AppException(errorCode.getHttpStatus(), null, null, errorCode, detail);
3434
}
3535

3636

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package ir.bigz.springbootreal.exception;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Builder;
5+
import lombok.Getter;
6+
import org.springframework.http.HttpStatus;
7+
8+
@Getter
9+
@AllArgsConstructor
10+
@Builder
11+
public class CustomExceptionType implements ExceptionType{
12+
HttpStatus httpStatus;
13+
int errorCode;
14+
String reasonMessage;
15+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package ir.bigz.springbootreal.exception;
2+
3+
import org.springframework.http.HttpStatus;
4+
5+
public interface ExceptionType {
6+
HttpStatus getHttpStatus();
7+
int getErrorCode();
8+
String getReasonMessage();
9+
}

src/main/java/ir/bigz/springbootreal/exception/HttpErrorCode.java

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,37 @@
22

33
import org.springframework.http.HttpStatus;
44

5-
public enum HttpErrorCode {
6-
7-
ERR_10700 (10700, "Invalid Entity For Persist", HttpStatus.BAD_REQUEST),
8-
ERR_10701 (10701, "process of the request has been error", HttpStatus.BAD_REQUEST),
9-
ERR_10702 (10702, "User Not Found", HttpStatus.NOT_FOUND),
10-
ERR_10703 (10703, "Invalid Entity For Update", HttpStatus.BAD_REQUEST),
11-
ERR_10704 (10704, "validation Error", HttpStatus.BAD_REQUEST),
12-
ERR_10705 (10705, "create query process has been error", HttpStatus.BAD_REQUEST);
13-
14-
private final int code;
15-
private final HttpStatus status;
16-
private final String reason;
17-
18-
HttpErrorCode(int code, String reason, HttpStatus status) {
19-
this.code = code;
20-
this.status = status;
21-
this.reason = reason;
5+
public enum HttpErrorCode implements ExceptionType{
6+
7+
INVALID_ENTITY_FOR_INSERT (10700, "invalid_entity_for_insert", HttpStatus.BAD_REQUEST),
8+
INTERNAL_ERROR (10701, "internal_server_error", HttpStatus.INTERNAL_SERVER_ERROR),
9+
USER_NOT_FOUND (10702, "user_not_found", HttpStatus.NOT_FOUND),
10+
INVALID_ENTITY_FOR_UPDATE (10703, "invalid_entity_for_update", HttpStatus.BAD_REQUEST),
11+
VALIDATION_ERROR (10704, "validation_error", HttpStatus.BAD_REQUEST),
12+
CREATE_QUERY_ERROR (10705, "create_query_error", HttpStatus.BAD_REQUEST);
13+
14+
private final CustomExceptionType customExceptionType;
15+
16+
HttpErrorCode(int errorCode, String reasonMessage, HttpStatus httpStatus) {
17+
this.customExceptionType = new CustomExceptionType(httpStatus, errorCode, reasonMessage);
18+
}
19+
20+
@Override
21+
public HttpStatus getHttpStatus() {
22+
return this.customExceptionType.httpStatus;
2223
}
2324

24-
public int getCode() {
25-
return code;
25+
@Override
26+
public int getErrorCode() {
27+
return this.customExceptionType.errorCode;
2628
}
2729

28-
public HttpStatus getStatus() {
29-
return status;
30+
@Override
31+
public String getReasonMessage() {
32+
return this.customExceptionType.reasonMessage;
3033
}
3134

32-
public String getReason() {
33-
return reason;
35+
public CustomExceptionType getCustomExceptionType() {
36+
return customExceptionType;
3437
}
3538
}

src/main/java/ir/bigz/springbootreal/exception/HttpExceptionHandler.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,20 @@
88
import java.time.ZoneId;
99
import java.time.ZonedDateTime;
1010
import java.time.format.DateTimeFormatter;
11+
import java.util.UUID;
1112

1213
@ControllerAdvice
1314
public class HttpExceptionHandler {
1415

1516
@ExceptionHandler(value = {AppException.class})
1617
public ResponseEntity<Object> handleApiRequestException(AppException e){
18+
UUID uuid = UUID.randomUUID();
1719

18-
HttpExceptionModel apiException = new HttpExceptionModel(e.getDetail(),
19-
e.getHttpErrorCode(),
20+
HttpExceptionModel apiException = new HttpExceptionModel(uuid.toString(),
21+
e.getHttpErrorCode().getErrorCode(), e.getDetail(),
2022
timeLog(), null);
2123

22-
return new ResponseEntity<>(apiException, e.getHttpErrorCode().getStatus());
24+
return new ResponseEntity<>(apiException, e.getHttpErrorCode().getHttpStatus());
2325
}
2426

2527
private String timeLog(){

src/main/java/ir/bigz/springbootreal/exception/HttpExceptionModel.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010
@Builder
1111
public class HttpExceptionModel {
1212

13+
private String uuid;
14+
private final int errorCode;
1315
private final String message;
14-
private final HttpErrorCode httpErrorCode;
1516
private final String timestamp;
1617
private final ValidationErrorResponseModel validationError;
1718
}

0 commit comments

Comments
 (0)