Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Refactor] feat: 전역 예외 처리 추가 및 URL 단축 로직 분리 #22

Merged
merged 9 commits into from
Feb 1, 2025

Conversation

seeeeeeong
Copy link
Collaborator

feat: 전역 예외 처리 추가 및 URL 단축 로직 분리

  1. 전역 예외 처리를 추가하였습니다.
  2. 패키지 구조를 변경하고 URL 단축 로직을 UrlShortener 컴포넌트로 분리하였습니다.

전역 예외 처리 추가

  1. GlobalExceptionHandler를 추가하여 NotFoundShortenUrlException 예외 발생 시 404 Not Found 응답을 반환하도록 처리하였습니다.
  2. NotFoundShortenUrlException 추가에 따라 테스트 코드를 수정하였습니다.

패키지 구조 변경 및 URL 단축 로직 분리

  1. 패키지 구조를 직관적으로 변경하였습니다.
  2. UrlShortenService에서 Key를 생성하는 로직을 컴포넌트로 분리하였습니다.
  • 추후 중복 관련 로직을 추가하고, Key 생성 방식을 변경할 예정입니다.
  1. request, response 클래스를 추가하였습니다.

@seeeeeeong seeeeeeong requested a review from Hyune-c January 31, 2025 08:22
@ExceptionHandler(NotFoundShortenUrlException.class)
public ResponseEntity<String> handleNotFoundShortenUrlException(NotFoundShortenUrlException ex) {
return new ResponseEntity<>("단축 URL을 찾지 못했습니다.", HttpStatus.NOT_FOUND);
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

문자열을 그대로 내려보내는게 의도된 것일까요?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

추후 정의되는 예외가 추가됨에 따라, json 형식의 에러 코드와 메시지를 포함한 응답을 생성할 예정입니다.

return String.valueOf(RANDOM.nextInt(10000));
}

}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 클래스가 꼭 bean 이어야 할까요?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

현재 UrlShortenService에서만 사용되기 때문에 빈 등록을 제거하였습니다.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 클래스가 꼭 객체로서 생성되어야 할까요?

Copy link
Collaborator Author

@seeeeeeong seeeeeeong Feb 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

URL 단축 방식이 변경되고 다양한 메서드가 추가될 가능성을 고려하여 객체로 생성하였습니다. 현재는 shorten() 메서드만 사용하기 때문에 객체로 생성하지 않아도 될 것 같아 static 메서드로 변경하였습니다.

UrlShortener 클래스에 shorten() 이외의 여러 가지 메서드가 추가된다면, 객체를 생성할지 말지는 클래스 내에 상태를 가지는지 여부에 따라 판단하는 것이 맞는지 궁금합니다.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

현재는 shorten() 메서드만 사용하기 때문에 객체로 생성하지 않아도 될 것 같습니다. 그렇다면 static 메서드로 구현하는 것이 맞을까요?

저는 지금의 상황이면 util 성격으로 간주하여 static method 로 만들 것 같습니다.

UrlShortener 클래스에 shorten() 이외의 여러 가지 메서드가 추가된다면,

미래는 불특정하기에 지금에 맞는 클래스 형태를 권장 합니다.

  • ex) 클래스 안에 메서드를 추가하는게 아니라, provider 를 여러개 만들어서 필요에 따라 사용한다.

객체를 생성할지 말지는 클래스 내에 상태를 가지는지 여부에 따라 판단하는 것이 맞는지 궁금합니다.

이건 질문 이해가 잘 안되네요. 상태 보유 여부를 떠나서 런타임에 dto 외의 객체를 생성하는 케이스는 거의 없지 않나요?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

객체를 생성할지 말지는 클래스 내에 상태를 가지는지 여부에 따라 판단하는 것이 맞는지 궁금합니다.

제가 이해를 잘못한 것 같습니다. static method로 사용하고 추후 구현에 따라 궁금한 점이 생기면 다시 질문드리겠습니다!

public GetOriginUrlResponse(String originUrl) {
this.originUrl = originUrl;
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

record 클래스를 알고 계신가요?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

record가 더 간결하고 가독성이 좋다고 생각되어 변경하였습니다.

@Hyune-c Hyune-c changed the title [step2] feat: 전역 예외 처리 추가 및 URL 단축 로직 분리 [Refactor] feat: 전역 예외 처리 추가 및 URL 단축 로직 분리 Feb 1, 2025
@seeeeeeong seeeeeeong requested a review from Hyune-c February 1, 2025 17:22
return String.valueOf(RANDOM.nextInt(10000));
}

}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 클래스가 꼭 객체로서 생성되어야 할까요?

@seeeeeeong seeeeeeong requested a review from Hyune-c February 1, 2025 17:47
@seeeeeeong seeeeeeong merged commit 1181b7e into whatever-mentoring:seeeeeeong Feb 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants