Skip to content

Commit

Permalink
feat: custom exception 예제 코드 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
EunjiShin committed Jul 5, 2024
1 parent 198af3c commit 77807ad
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.depromeet.spot.common.exception.member;

import org.depromeet.spot.common.exception.ErrorCode;
import org.springframework.http.HttpStatus;

import lombok.Getter;

@Getter
public enum MemberErrorCode implements ErrorCode {
MEMBER_NOT_FOUND(HttpStatus.NOT_FOUND, "M001", "요청 유저가 존재하지 않습니다."),
;

private final HttpStatus status;
private final String code;
private String message;

MemberErrorCode(HttpStatus status, String code, String message) {
this.status = status;
this.code = code;
this.message = message;
}

public MemberErrorCode appended(Object o) {
message = message + " {" + o.toString() + "}";
return this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.depromeet.spot.common.exception.member;

import org.depromeet.spot.common.exception.BusinessException;

public abstract class MemberException extends BusinessException {

protected MemberException(MemberErrorCode errorCode) {
super(errorCode);
}

public static class MemberNotFoundException extends MemberException {
public MemberNotFoundException() {
super(MemberErrorCode.MEMBER_NOT_FOUND);
}

public MemberNotFoundException(Object o) {
super(MemberErrorCode.MEMBER_NOT_FOUND.appended(o));
}
}
}
1 change: 1 addition & 0 deletions usecase/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
dependencies {
implementation(project(":common"))
implementation(project(":domain"))

// spring
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

import java.util.List;

import org.depromeet.spot.common.exception.member.MemberException.MemberNotFoundException;
import org.depromeet.spot.domain.member.Member;
import org.depromeet.spot.usecase.port.in.MemberUsecase;
import org.depromeet.spot.usecase.port.out.MemberRepository;
import org.springframework.stereotype.Service;

import lombok.RequiredArgsConstructor;
import lombok.val;

@Service
@RequiredArgsConstructor
Expand All @@ -18,12 +18,16 @@ public class MemberService implements MemberUsecase {

@Override
public Member create(final String name) {
val member = new Member(null, name);
var member = new Member(null, name);
return memberRepository.save(member);
}

@Override
public List<Member> findByName(final String name) {
return memberRepository.findByName(name);
var members = memberRepository.findByName(name);
if (members.isEmpty()) {
throw new MemberNotFoundException(name);
}
return members;
}
}

0 comments on commit 77807ad

Please sign in to comment.