Skip to content

Commit 6b3bb7d

Browse files
authored
Merge pull request #5 from WellingtonCosta/master
Making this a Kotlin library.
2 parents 316f990 + bc6fefd commit 6b3bb7d

File tree

29 files changed

+169
-259
lines changed

29 files changed

+169
-259
lines changed

README.md

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,47 +2,51 @@
22

33
Rest Error Interceptor
44
===================
5-
This will intercept the codes responses and throw the proper exception. </br>
5+
6+
This interceptor provides an easy way to handle the most common REST HTTP status code as exceptions.
67

78
Example
89
--------
910

1011
```kotlin
11-
Try {
12-
repository.makeRestRequest()
13-
14-
} catch (throwable: Throwable){
15-
if(throwable is Unauthorizad){
16-
// Make login again.
17-
}
12+
try {
13+
repository.makeRestRequest()
14+
} catch (throwable: Throwable) {
15+
when(throwable) {
16+
BadRequest -> { } // Handle the bad request status code
17+
InternalServerError -> { } // Handle the internal server error status code
18+
Unauthorized -> { } // Handle the unauthorized status code
19+
}
1820
}
1921
```
2022

2123
How to
2224
------
2325

24-
```java
25-
RestErrorInterceptor errorInterceptor = new RestErrorInterceptor();
26-
OkHttpClient client = new OkHttpClient.Builder()
27-
.addInterceptor(errorInterceptor)
28-
.build();
26+
```kotlin
27+
val errorInterceptor = RestErrorInterceptor()
28+
val client = OkHttpClient.Builder()
29+
.addInterceptor(errorInterceptor)
30+
.build()
2931
```
3032

3133
Download
3234
--------
33-
Add it in your root build.gradle at the end of repositories:
35+
1- Add the Jitpack Repository in your root build.gradle file:
36+
3437
```groovy
35-
allprojects {
36-
repositories {
37-
...
38-
maven { url 'https://jitpack.io' }
39-
}
40-
}
38+
allprojects {
39+
repositories {
40+
...
41+
maven { url 'https://jitpack.io' }
42+
}
43+
}
4144
```
4245

43-
Add the dependency
46+
2- Add the dependency in your project-level build.gradle file:
47+
4448
```groovy
45-
dependencies {
46-
implementation 'com.github.jeancsanchez:okhttp-rest-error-interceptor:{latest version}'
47-
}
48-
```
49+
dependencies {
50+
implementation 'com.github.jeancsanchez:okhttp-rest-error-interceptor:{latest version}'
51+
}
52+
```

build.gradle

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,16 @@
1-
// Top-level build file where you can add configuration options common to all sub-projects/modules.
2-
31
buildscript {
4-
ext.kotlin_version = '1.3.21'
52
repositories {
6-
google()
73
jcenter()
84

95
}
106
dependencies {
11-
classpath 'com.android.tools.build:gradle:3.3.2'
12-
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
13-
// NOTE: Do not place your application dependencies here; they belong
14-
// in the individual module build.gradle files
7+
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.21"
158
}
169
}
1710

1811
allprojects {
1912
repositories {
20-
google()
2113
jcenter()
22-
2314
}
2415
}
2516

errorinterceptor/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
/build
2+
/out

errorinterceptor/build.gradle

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,12 @@
1-
apply plugin: 'com.android.library'
2-
apply plugin: 'kotlin-android'
3-
apply plugin: 'kotlin-android-extensions'
1+
apply plugin: 'maven'
2+
apply plugin: 'org.jetbrains.kotlin.jvm'
43

5-
android {
6-
compileSdkVersion 28
7-
defaultConfig {
8-
minSdkVersion 14
9-
targetSdkVersion 28
10-
}
11-
12-
buildTypes {
13-
release {
14-
minifyEnabled false
15-
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
16-
}
17-
}
18-
}
4+
group = 'com.github.jeancsanchez'
195

206
dependencies {
21-
implementation fileTree(dir: 'libs', include: ['*.jar'])
7+
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.21"
8+
implementation "com.squareup.okhttp3:okhttp:3.14.0"
229

23-
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
24-
implementation("com.squareup.okhttp3:okhttp:3.14.0")
25-
testImplementation 'junit:junit:4.12'
26-
androidTestImplementation 'com.android.support.test:runner:1.0.2'
10+
testImplementation "junit:junit:4.12"
11+
testImplementation "com.nhaarman.mockitokotlin2:mockito-kotlin:2.1.0"
2712
}

errorinterceptor/proguard-rules.pro

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

errorinterceptor/src/main/AndroidManifest.xml

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

errorinterceptor/src/main/java/br/com/jeancsanchez/restinterceptor/ErrorInterceptor.kt

Lines changed: 0 additions & 32 deletions
This file was deleted.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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 = chain.request().let {
16+
chain.proceed(it).apply {
17+
when(code()) {
18+
400 -> throw BadRequest
19+
401 -> throw Unauthorized
20+
403 -> throw Forbidden
21+
404 -> throw NotFound
22+
405 -> throw MethodNotAllowed
23+
500 -> throw InternalServerError
24+
502 -> throw BadGateway
25+
503 -> throw ServiceUnavailable
26+
}
27+
}
28+
}
29+
}

errorinterceptor/src/main/res/drawable-v24/ic_launcher_foreground.xml

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

errorinterceptor/src/main/res/drawable/ic_launcher_background.xml

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

0 commit comments

Comments
 (0)