@@ -2,42 +2,54 @@ package com.kuit.findu.data.dataremote.util
22
33import com.google.firebase.crashlytics.FirebaseCrashlytics
44import com.google.firebase.crashlytics.recordException
5+ import kotlinx.coroutines.CoroutineScope
6+ import kotlinx.coroutines.Dispatchers
7+ import kotlinx.coroutines.launch
58import okhttp3.Interceptor
69import okhttp3.Response
710import javax.inject.Inject
811
912class ErrorTrackingInterceptor @Inject constructor(
1013 private val firebaseCrashlytics : FirebaseCrashlytics ,
14+ private val discordLogger : DiscordLogger ,
1115) : Interceptor {
16+
1217 override fun intercept (chain : Interceptor .Chain ): Response {
1318 val request = chain.request()
1419 val response: Response
1520
1621 try {
1722 response = chain.proceed(request)
1823 } catch (e: Exception ) {
19- // 1. 네트워크 자체가 끊긴 경우 등 (IOException)
2024 firebaseCrashlytics.recordException(e)
2125 throw e
2226 }
2327
24- // 2. 서버에서 응답은 왔으나 4xx, 5xx 에러인 경우
2528 if (! response.isSuccessful) {
2629 val code = response.code
27-
2830 val url = request.url.toString()
2931
30- // Crashlytics에 상세 정보 기록
31- val exceptionMessage = when (response.code) {
32- in (400 .. 499 ) -> " Client $code Error"
33- in (500 .. 599 ) -> " Server $code Error"
32+ val exceptionMessage = when (code) {
33+ in 400 .. 499 -> " Client $code Error"
34+ in 500 .. 599 -> " Server $code Error"
3435 else -> " Unknown $code Error"
3536 }
37+
3638 firebaseCrashlytics.recordException(Exception (exceptionMessage)) {
3739 key(" api_method" , request.method)
3840 key(" api_url" , url)
3941 key(" api_status" , code)
4042 }
43+
44+ if (code in 500 .. 599 ) {
45+ CoroutineScope (Dispatchers .IO ).launch {
46+ discordLogger.logServerError(
47+ code = code,
48+ method = request.method,
49+ url = url
50+ )
51+ }
52+ }
4153 }
4254
4355 return response
0 commit comments