Skip to content

Commit

Permalink
テスト時の警告解除
Browse files Browse the repository at this point in the history
  • Loading branch information
rnakagawa16 committed Nov 14, 2024
1 parent f2e4b5c commit 3d04850
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

/**
* サーバーエラーのハンドリングを行うクラスです(本番環境用)
* サーバーエラーのハンドリングを行うクラスです。
*/
@ControllerAdvice(basePackages = "com.dressca")
public class ExceptionHandlerControllerAdvice extends ResponseEntityExceptionHandler {
Expand All @@ -30,7 +30,7 @@ public class ExceptionHandlerControllerAdvice extends ResponseEntityExceptionHan
private ProblemDetailsCreation problemDetailsCreation;

/**
* その他の業務エラーをステータースコード500で返却する(本番環境、テスト環境用)
* その他の業務エラーをステータースコード500で返却する。
*
* @param e 業務例外
* @param req リクエスト
Expand All @@ -49,16 +49,15 @@ public ResponseEntity<ProblemDetail> handleLogicException(LogicException e, Http
}

/**
* その他のシステムエラーをステータースコード500で返却する(本番環境、テスト環境用)
* その他のシステムエラーをステータースコード500で返却する。
*
* @param e その他の例外
* @param req リクエスト
* @return ステータースコード500のレスポンス
*/
@ExceptionHandler(SystemException.class)
public ResponseEntity<ProblemDetail> handleSystemException(SystemException e, HttpServletRequest req) {
String[] test = { "test", "tokyo" };
ErrorMessageBuilder errorBuilder = new ErrorMessageBuilder(e, CommonExceptionIdConstant.E_SYSTEM, null, test);
ErrorMessageBuilder errorBuilder = new ErrorMessageBuilder(e, CommonExceptionIdConstant.E_SYSTEM, null, null);
apLog.error(errorBuilder.createLogMessageStackTrace());
ProblemDetail problemDetail = problemDetailsCreation.createProblemDetail(errorBuilder,
ProblemDetailsConstant.SYSTEM_ERROR_TITLE,
Expand All @@ -69,16 +68,15 @@ public ResponseEntity<ProblemDetail> handleSystemException(SystemException e, Ht
}

/**
* 上記のいずれにも当てはまらない例外をステータースコード500で返却する(本番環境、テスト環境用)
* 上記のいずれにも当てはまらない例外をステータースコード500で返却する。
*
* @param e その他の例外
* @param req リクエスト
* @return ステータースコード500のレスポンス
*/
@ExceptionHandler(Exception.class)
public ResponseEntity<ProblemDetail> handleException(Exception e, HttpServletRequest req) {
String[] test = { "test", "tokyo" };
ErrorMessageBuilder errorBuilder = new ErrorMessageBuilder(e, CommonExceptionIdConstant.E_SYSTEM, null, test);
ErrorMessageBuilder errorBuilder = new ErrorMessageBuilder(e, CommonExceptionIdConstant.E_SYSTEM, null, null);
apLog.error(errorBuilder.createLogMessageStackTrace());
ProblemDetail problemDetail = problemDetailsCreation.createProblemDetail(errorBuilder,
ProblemDetailsConstant.SYSTEM_ERROR_TITLE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,49 @@
import com.dressca.web.constant.ProblemDetailsConstant;
import com.dressca.web.log.ErrorMessageBuilder;

/**
* エラーレスポンスに含める ProblemDetails を作成するクラスです。
*/
@Component
public class ProblemDetailsCreation {

@Autowired
private Environment env;

/**
* その他のシステムエラーをステータースコード500で返却する。
*
* @param errorBuilder 例外ビルダー
* @param title タイトル
* @param status ステータスコード
* @return エラーレスポンスに格納する ProblemDetails
*/
public ProblemDetail createProblemDetail(ErrorMessageBuilder errorBuilder, String title, HttpStatus status) {

ProblemDetail problemDetail = ProblemDetail.forStatus(status);

problemDetail.setTitle(title);

String[] activeProfiles = env.getActiveProfiles();
if (activeProfiles.length == 0) {
activeProfiles = env.getDefaultProfiles();
}

// local 環境においては detail を含める
if (Arrays.stream(activeProfiles).filter(profile -> Objects.equals(profile, "local"))
.findFirst().isPresent()) {
problemDetail.setDetail(errorBuilder.createLogMessageStackTrace());
}

Map<String, Object> errorProperty = new LinkedHashMap<String, Object>() {
{
put(ProblemDetailsConstant.EXCEPTION_ID, errorBuilder.getExceptionId());
put(ProblemDetailsConstant.EXCEPTION_VALUES, errorBuilder.getFrontMessageValue());
}
};

ProblemDetail problemDetail = ProblemDetail.forStatus(status);
// local 環境においては details を含める
if (Arrays.stream(env.getActiveProfiles()).filter(profile -> Objects.equals(profile, "local"))
.findFirst().isPresent()) {
problemDetail.setDetail(errorBuilder.createLogMessageStackTrace());
}
problemDetail.setTitle(title);
problemDetail.setProperties(errorProperty);

return problemDetail;
}
}

0 comments on commit 3d04850

Please sign in to comment.