Skip to content

Commit b6ad9a6

Browse files
committed
Adding the interceptor and errors
1 parent 3d14605 commit b6ad9a6

File tree

14 files changed

+135
-43
lines changed

14 files changed

+135
-43
lines changed

errorinterceptor/build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ android {
1919

2020
dependencies {
2121
implementation fileTree(dir: 'libs', include: ['*.jar'])
22-
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
2322

23+
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
24+
implementation("com.squareup.okhttp3:okhttp:3.14.0")
2425
testImplementation 'junit:junit:4.12'
2526
androidTestImplementation 'com.android.support.test:runner:1.0.2'
2627
}

errorinterceptor/src/androidTest/java/br/org/inec/resterrorinteceptor/ExampleInstrumentedTest.kt

Lines changed: 0 additions & 24 deletions
This file was deleted.

errorinterceptor/src/main/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
2-
package="br.org.inec.resterrorinteceptor">
2+
package="br.com.jeancsanchez.restinterceptor">
33

44
<application android:allowBackup="true"
55
android:label="@string/app_name"
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package br.com.jeancsanchez.restinterceptor
2+
3+
import br.com.jeancsanchez.restinterceptor.errors.*
4+
import okhttp3.Interceptor
5+
import okhttp3.Response
6+
7+
/**
8+
* @author Jean Carlos (Github: @jeancsanchez)
9+
* @date 04/04/19.
10+
* Jesus is alive!
11+
*/
12+
13+
class RestErrorInterceptor : Interceptor {
14+
15+
override fun intercept(chain: Interceptor.Chain): Response {
16+
val request = chain.request()
17+
val response = chain.proceed(request)
18+
19+
when (response.code()) {
20+
400 -> throw BadRequest
21+
401 -> throw Unauthorized
22+
403 -> throw Forbidden
23+
404 -> throw NotFound
24+
405 -> throw MethodNotAllowed
25+
500 -> throw InternalServerError
26+
502 -> throw BadGateway
27+
503 -> throw ServiceUnavailable
28+
}
29+
30+
return response
31+
}
32+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package br.com.jeancsanchez.restinterceptor.errors
2+
3+
/**
4+
* The server, while acting as a gateway or proxy, received an invalid response from the upstream
5+
* server it accessed in attempting to fulfill the request.
6+
* <a href="https://www.restapitutorial.com/httpstatuscodes.html">restapitutorial.com <a/>
7+
*
8+
* @author Jean Carlos (Github: @jeancsanchez)
9+
* @date 16/01/19.
10+
* Jesus is alive!
11+
*/
12+
object BadGateway : Error(502, "Bad Gateway")
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package br.com.jeancsanchez.restinterceptor.errors
2+
3+
/**
4+
* The request could not be understood by the server due to malformed syntax.
5+
* The client SHOULD NOT repeat the request without modifications.
6+
* <a href="https://www.restapitutorial.com/httpstatuscodes.html">restapitutorial.com <a/>
7+
*
8+
* @author Jean Carlos (Github: @jeancsanchez)
9+
* @date 16/01/19.
10+
* Jesus is alive!
11+
*/
12+
object BadRequest : Error(400, "Bad Request")
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package br.com.jeancsanchez.restinterceptor.errors
2+
3+
/**
4+
* @author Jean Carlos (Github: @jeancsanchez)
5+
* @date 04/04/19.
6+
* Jesus is alive!
7+
*/
8+
open class Error(code: Int, msg: String) : Throwable("$code: $msg")
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package br.com.jeancsanchez.restinterceptor.errors
2+
3+
/**
4+
* The server understood the request, but is refusing to fulfill it. Authorization will not help
5+
* and the request SHOULD NOT be repeated.
6+
* <a href="https://www.restapitutorial.com/httpstatuscodes.html">restapitutorial.com <a/>
7+
*
8+
* @author Jean Carlos (Github: @jeancsanchez)
9+
* @date 16/01/19.
10+
* Jesus is alive!
11+
*/
12+
object Forbidden : Error(403, "Forbidden")
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package br.com.jeancsanchez.restinterceptor.errors
2+
3+
/**
4+
* The server encountered an unexpected condition which prevented it from fulfilling the request.
5+
* <a href="https://www.restapitutorial.com/httpstatuscodes.html">restapitutorial.com <a/>
6+
*
7+
* @author Jean Carlos (Github: @jeancsanchez)
8+
* @date 16/01/19.
9+
* Jesus is alive!
10+
*/
11+
object InternalServerError : Error(500, "Internal Server Error")
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package br.com.jeancsanchez.restinterceptor.errors
2+
3+
/**
4+
* The method specified in the Request-Line is not allowed for the resource identified by the Request-URI.
5+
* The response MUST include an Allow header containing a list of valid methods for the requested resource.
6+
* <a href="https://www.restapitutorial.com/httpstatuscodes.html">restapitutorial.com <a/>
7+
*
8+
* @author Jean Carlos (Github: @jeancsanchez)
9+
* @date 16/01/19.
10+
* Jesus is alive!
11+
*/
12+
object MethodNotAllowed : Error(405, "Method Not Allowed")

0 commit comments

Comments
 (0)