-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
myung jun Hyun
committed
Nov 25, 2019
1 parent
151ee1f
commit e4b8eca
Showing
11 changed files
with
114 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.mashup.api | ||
|
||
data class ErrorResponse( | ||
val code: String, | ||
val message: String? | ||
) { | ||
fun hasErrors(): Boolean { | ||
return !message.isNullOrEmpty() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ice/remote/response/NoticeListResponse.kt → ...api/notice/response/NoticeListResponse.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package com.mashup.util | ||
|
||
import com.google.gson.Gson | ||
import com.google.gson.JsonParseException | ||
import com.mashup.api.ErrorResponse | ||
import okhttp3.Interceptor | ||
import okhttp3.Response | ||
import java.io.IOException | ||
import java.nio.charset.Charset | ||
|
||
private val UTF8 = Charset.forName("UTF-8") | ||
|
||
class ErrorInterceptor : Interceptor { | ||
@Throws(IOException::class) | ||
override fun intercept(chain: Interceptor.Chain): Response { | ||
val originalRequest = chain.request() | ||
val response = chain.proceed(originalRequest) | ||
|
||
return if (response.code() >= 400) { | ||
throwError(response) | ||
response | ||
} else { | ||
response | ||
} | ||
} | ||
|
||
@Throws(IOException::class) | ||
private fun throwError(response: Response) { | ||
val responseBody = response.body() | ||
if (responseBody != null) { | ||
val source = responseBody.source() | ||
source.request(java.lang.Long.MAX_VALUE) // Buffer the entire body. | ||
val buffer = source.buffer() | ||
|
||
var charset = UTF8 | ||
val contentType = responseBody.contentType() | ||
if (contentType != null) { | ||
charset = contentType.charset(UTF8) | ||
} | ||
|
||
if (responseBody.contentLength() != 0L) { | ||
val responseJSON = buffer.clone().readString(charset) | ||
val message: String? | ||
try { | ||
message = Gson().fromJson(responseJSON, ErrorResponse::class.java).message | ||
throw Throwable(message) | ||
} catch (error: JsonParseException) { | ||
throw Throwable("") | ||
} | ||
} | ||
} else { | ||
throw Throwable("") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters