Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/main/java/kr/allcll/backend/domain/user/AuthFacade.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,15 @@ public class AuthFacade {
@Transactional
public LoginResult login(LoginRequest loginRequest) {
OkHttpClient client = authService.login(loginRequest);
toscAuthService.loginTosc(client, loginRequest);

boolean toscLoginFailed = false;
try {
toscAuthService.loginTosc(client, loginRequest);
} catch (Exception exception) {
toscLoginFailed = true;
log.error("[TOSC] 로그인 실패, TOSC 정보 업데이트 불가 (학번: {}): {}",
loginRequest.studentId(), exception.getMessage());
}
Comment on lines +32 to +38
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

toscAuthService.loginTosc 메서드 내에 에러를 던지는 로직이 없는 것 같은데, catch문이 어떻게 작동되는 지 궁금합니다!🤔

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

그러네요 제가 다 잡아버리고 catch 문을 작동시켰군요 😇
덕분에 확인 후 수정했습니다!!


UserInfo userInfo = userFetcher.fetch(client);
User user = userService.findOrCreate(userInfo);
Expand All @@ -36,9 +44,10 @@ public LoginResult login(LoginRequest loginRequest) {
GraduationCertInfo certInfo = graduationCertResolver.resolve(user, client);
graduationCertService.createOrUpdate(user, certInfo);
} catch (Exception exception) {
log.error("[GraduationCert] 인증 정보 저장 중 오류 발생 (유저 ID: {}): {}", user.getId(), exception.getMessage());
log.error("[GraduationCert] 인증 정보 저장 중 오류 발생 (유저 ID: {}): {}",
user.getId(), exception.getMessage());
}

return new LoginResult(user.getId());
return new LoginResult(user.getId(), toscLoginFailed);
}
}
33 changes: 29 additions & 4 deletions src/main/java/kr/allcll/backend/domain/user/ToscAuthService.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
package kr.allcll.backend.domain.user;

import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.net.SocketTimeoutException;
import kr.allcll.backend.client.LoginProperties;
import kr.allcll.backend.domain.user.dto.LoginRequest;
import kr.allcll.backend.support.exception.AllcllErrorCode;
import kr.allcll.backend.support.exception.AllcllException;
import kr.allcll.backend.domain.user.dto.ToscResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;
import org.springframework.stereotype.Service;

@Slf4j
@Service
@RequiredArgsConstructor
public class ToscAuthService {

private final LoginProperties properties;
private final ObjectMapper objectMapper;

public void loginTosc(OkHttpClient client, LoginRequest loginRequest) {
RequestBody body = new FormBody.Builder()
Expand All @@ -32,10 +37,30 @@ public void loginTosc(OkHttpClient client, LoginRequest loginRequest) {

try (Response response = client.newCall(request).execute()) {
if (!response.isSuccessful()) {
throw new AllcllException(AllcllErrorCode.TOSC_LOGIN_FAIL);
log.error("[TOSC] HTTP 요청 실패 (상태 코드: {}, 학번: {})",
response.code(), loginRequest.studentId());
return;
}

ResponseBody responseBody = response.body();
if (responseBody == null) {
log.error("[TOSC] 응답 본문이 비어있음 (학번: {})", loginRequest.studentId());
return;
}

String responseString = responseBody.string();
ToscResponse toscResponse = objectMapper.readValue(responseString, ToscResponse.class);

if (!toscResponse.success()) {
log.error("[TOSC] 로그인 실패 (학번: {}, 에러: {})",
loginRequest.studentId(), toscResponse.getErrorMessage());
}
} catch (SocketTimeoutException exception) {
log.error("[TOSC] 타임아웃 발생 (학번: {})", loginRequest.studentId(), exception);
} catch (IOException exception) {
throw new AllcllException(AllcllErrorCode.TOSC_LOGIN_IO_ERROR);
log.error("[TOSC] I/O 오류 발생 (학번: {})", loginRequest.studentId(), exception);
} catch (Exception exception) {
log.error("[TOSC] 예상치 못한 오류 발생 (학번: {})", loginRequest.studentId(), exception);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package kr.allcll.backend.domain.user.dto;

public record LoginResult(
Long userId
Long userId,
boolean toscLoginFailed
) {

}
18 changes: 18 additions & 0 deletions src/main/java/kr/allcll/backend/domain/user/dto/ToscResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package kr.allcll.backend.domain.user.dto;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import java.util.Map;

@JsonIgnoreProperties(ignoreUnknown = true)
public record ToscResponse(
boolean success,
Map<String, String> errors
) {

public String getErrorMessage() {
if (errors == null || errors.isEmpty()) {
return "알 수 없는 오류";
}
return String.join(", ", errors.values());
}
}
Loading