Skip to content

Commit

Permalink
✨ feat(MenusService, SessionConfiguration): Add error handling an…
Browse files Browse the repository at this point in the history
…d improve exception messages for menu operations.
  • Loading branch information
vnobo committed Dec 18, 2024
1 parent 0d395f3 commit 32c614e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.plate.boot.config;

import com.nimbusds.oauth2.sdk.dpop.verifiers.AccessTokenValidationException;
import com.plate.boot.commons.exception.RestServerException;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand Down Expand Up @@ -35,7 +36,7 @@
*/
@Configuration(proxyBeanMethods = false)
@EnableRedisIndexedWebSession
public class SessionConfiguration{
public class SessionConfiguration {

public static final String HEADER_SESSION_ID_NAME = "X-Auth-Token";
public static final String X_REQUESTED_WITH = "X-Requested-With";
Expand Down Expand Up @@ -112,7 +113,7 @@ private static String resolveAccessTokenFromRequest(ServerHttpRequest request) {
}

private static RestServerException invalidTokenError(String message) {
return RestServerException.withMsg("Bearer token is malformed!", message);
return RestServerException.withMsg(message, new AccessTokenValidationException("Bearer token is malformed"));
}

@Override
Expand Down Expand Up @@ -210,7 +211,8 @@ private String resolveFromAuthorizationHeader(HttpHeaders headers) {
}
Matcher matcher = AUTHORIZATION_PATTERN.matcher(authorization);
if (!matcher.matches()) {
throw invalidTokenError("bearer is malformed.");
throw invalidTokenError("Extracts the bearer token from the" +
" 'Authorization' header within the provided HTTP headers.");
}
return matcher.group("token");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,19 @@ public Mono<Menu> add(MenuRequest request) {
var existsMono = this.entityTemplate.exists(Query.query(criteria), Menu.class);
existsMono = existsMono.filter(isExists -> !isExists);
existsMono = existsMono.switchIfEmpty(Mono.error(RestServerException
.withMsg("Add menu[" + request.getName() + "] is exists!",
"The menu already exists, please try another name. is params: " + criteria)));
.withMsg("Add menu[" + request.getName() + "] is exists",
new IllegalArgumentException("The menu already exists, please try another name. is params: "
+ criteria))));
return existsMono.flatMap((b) -> this.operate(request));
}

public Mono<Menu> modify(MenuRequest request) {
log.debug("Menu modify request: {}", request);
var oldMunuMono = this.menusRepository.findByCode(request.getCode())
.switchIfEmpty(Mono.error(RestServerException.withMsg(
"Modify menu [" + request.getName() + "] is empty!",
"The menu does not exist, please choose another name. is code: " + request.getCode())));
"Modify menu [" + request.getName() + "] is empty",
new IllegalArgumentException("The menu does not exist, please choose another name. is code: "
+ request.getCode()))));
oldMunuMono = oldMunuMono.flatMap(old -> {
request.setId(old.getId());
request.setAuthority(old.getAuthority());
Expand Down

0 comments on commit 32c614e

Please sign in to comment.