Skip to content

Commit 218ce90

Browse files
committed
Merge branch 'v1.0.0-dev'
2 parents ffa198e + 9e5188e commit 218ce90

17 files changed

Lines changed: 355 additions & 161 deletions

File tree

ChangeLog.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# ChangeLog
2+
3+
## v1.0.0
4+
5+
### New
6+
7+
- `errorHandler.run(BlockExecutor)` saves you from a `try/catch` block or two
8+
```java
9+
try {
10+
doSomething();
11+
} catch(Exception ex) {
12+
errorHandler.handle(ex);
13+
}
14+
15+
// can now be written as
16+
17+
errorHandler.run(() -> doSomething())
18+
```
19+
20+
### Breaking
21+
22+
- `bindErrorCode` renamed to `bind`
23+
- `bindErrorCodeClass` renamed to `bindClass`
24+
25+
26+
27+
28+

README.md

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,23 @@
55

66
> Error handling library for Android and Java
77
8+
Encapsulate error handling logic into objects that adhere to configurable defaults. Then pass them around as parameters or inject them via DI.
9+
810
## Download
911
Download the [latest JAR](https://bintray.com/workable/maven/ErrorHandler/_latestVersion) or grab via Maven:
1012
```xml
1113
<dependency>
1214
<groupId>com.workable</groupId>
1315
<artifactId>error-handler</artifactId>
14-
<version>0.9.1</version>
16+
<version>1.0.0</version>
1517
<type>pom</type>
1618
</dependency>
1719
```
1820

1921
or Gradle:
2022

2123
```groovy
22-
compile 'com.workable:error-handler:0.9.1'
24+
compile 'com.workable:error-handler:1.0.0'
2325
```
2426

2527

@@ -42,22 +44,22 @@ ErrorHandler
4244
.defaultErrorHandler()
4345

4446
// Bind certain exceptions to "offline"
45-
.bindErrorCode("offline", errorCode -> throwable -> {
47+
.bind("offline", errorCode -> throwable -> {
4648
return throwable instanceof UnknownHostException || throwable instanceof ConnectException;
4749
})
4850

4951
// Bind HTTP 404 status to 404
50-
.bindErrorCode(404, errorCode -> throwable -> {
52+
.bind(404, errorCode -> throwable -> {
5153
return ((HttpException) throwable).code() == 404;
5254
})
5355

5456
// Bind HTTP 500 status to 500
55-
.bindErrorCode(500, errorCode -> throwable -> {
57+
.bind(500, errorCode -> throwable -> {
5658
return ((HttpException) throwable).code() == 500;
5759
})
5860

5961
// Bind all DB errors to a custom enumeration
60-
.bindErrorCodeClass(DBError.class, errorCode -> throwable -> {
62+
.bindClass(DBError.class, errorCode -> throwable -> {
6163
return DBError.from(throwable) == errorCode;
6264
})
6365

@@ -100,6 +102,12 @@ try {
100102
}
101103
```
102104

105+
### Run blocks of code using ErrorHandler.run
106+
107+
```java
108+
ErrorHandler.run(() -> fetchNewMessages());
109+
```
110+
103111
### Override defaults when needed
104112

105113
```java
@@ -150,7 +158,7 @@ ErrorHandler is __thread-safe__.
150158

151159
* `on(Class<? extends Exception>, Action)` Register an _Action_ to be executed if error is an instance of `Exception`.
152160

153-
* `on(T, Action)` Register an _Action_ to be executed if error is bound to T, through `bindErrorCode()` or `bindErrorCodeClass()`.
161+
* `on(T, Action)` Register an _Action_ to be executed if error is bound to T, through `bind()` or `bindClass()`.
154162

155163
* `otherwise(Action)` Register an _Action_ to be executed only if no other _Action_ gets executed.
156164

@@ -162,9 +170,9 @@ ErrorHandler is __thread-safe__.
162170

163171
* `skipDefaults()` Skip any default actions. Meaning any actions registered on the `defaultErrorHandler` instance.
164172

165-
* `bindErrorCode(T, MatcherFactory<T>)` Bind instances of _T_ to match errors through a matcher provided by _MatcherFactory_.
173+
* `bind(T, MatcherFactory<T>)` Bind instances of _T_ to match errors through a matcher provided by _MatcherFactory_.
166174

167-
* `bindErrorCodeClass(Class<T>, MatcherFactory<T>)` Bind class _T_ to match errors through a matcher provided by _MatcherFactory_.
175+
* `bindClass(Class<T>, MatcherFactory<T>)` Bind class _T_ to match errors through a matcher provided by _MatcherFactory_.
168176

169177
* `clear()` Clear all registered _Actions_.
170178

@@ -174,23 +182,18 @@ ErrorHandler is __thread-safe__.
174182

175183

176184
## About
177-
A common problem in software, specially in UI software is that of error handling.
178-
179-
One way to classify errors can be by how they relate to the [problem domain](https://en.wikipedia.org/wiki/Problem_domain). Some errors, like `network` or `database` errors are orthogonal to the problem domain and designate truly **exceptional conditions**, while others are core parts of the domain like `validation` and `authentication` errors.
180-
181-
Another way to classify errors is by their **scope**. Are they **common** throughout the application or **specific** to a single screen, object or even method? Think of `UnauthorizedException` versus `InvalidPasswordException`.
182-
183-
And let's not forget another very simple distinction between errors. Those that are known at authoring time and thus **expected** (despite of how probable is that they occur), and those that are **unknown** until runtime.
184185

185-
With that in mind, we usually want to:
186+
When designing for errors, we usually need to:
186187

187-
1. have a **default** handler for every **expected** (exceptional, common or not) error
188-
2. handle **specific** errors **as appropriate** based on where and when they occur
189-
3. have a **default** catch-all handler for **unknown** errors
190-
4. **override** any default handler if needed
191-
5. keep our code **DRY**
188+
1. have a **default** handler for every **expected** error
189+
// i.e. network, subscription errors
190+
2. handle **specific** errors **as appropriate** based on where and when they occur
191+
// i.e. network error while uploading a file, invalid login
192+
3. have a **catch-all** handler for **unknown** errors
193+
// i.e. system libraries runtime errors we don't anticipate
194+
4. keep our code **DRY**
192195

193-
Java, as a language, provides you with a way to do the above. By mapping exceptional or very common errors to runtime exceptions and catching them lower in the call stack, while having specific expected errors mapped to checked exceptions and handle them near where the error occurred. Still, countless are the projects where this simple strategy has gone astray with lots of errors being either swallowed or left for the catch-all `Thread.UncaughtExceptionHandler`. Moreover, it usually comes with significant boilerplate code. `ErrorHandler` however eases this practice through its fluent API, error aliases and defaults mechanism.
196+
Java, as a language, provides you with a way to do the above. By mapping cross-cutting errors to runtime exceptions and catching them lower in the call stack, while having specific expected errors mapped to checked exceptions and handle them near where the error occurred. Still, countless are the projects where this simple strategy has gone astray with lots of errors being either swallowed or left for the catch-all `Thread.UncaughtExceptionHandler`. Moreover, it usually comes with significant boilerplate code. `ErrorHandler` however eases this practice through its fluent API, error aliases and defaults mechanism.
194197

195198
This library doesn't try to solve Java specific problems, although it does help with the `log and shallow` anti-pattern as it provides an opinionated and straightforward way to act inside every `catch` block. It was created for the needs of an Android app and proved itself useful very quickly. So it may work for you as well. If you like the concept and you're developing in _Swift_ or _Javascript_, we're baking 'em and will be available really soon.
196199

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
group 'com.workable'
2-
version '0.9'
2+
version '1.0.0'
33

44
apply plugin: 'java'
55

errorhandler-matchers/retrofit-rx-matcher/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
group 'com.workable'
2-
version '0.9'
2+
version '1.0.0'
33

44
apply plugin: 'java'
55

errorhandler-matchers/retrofit-rx-matcher/src/main/java/com/workable/errorhandler/matchers/retrofit/Range.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ public class Range {
1010

1111
/**
1212
* Creates a Range object with lower and upper bound
13-
* @param lowerBound
14-
* @param upperBound
15-
* @return
13+
* @param lowerBound lower limit of Range
14+
* @param upperBound upper limit of Range
15+
*
16+
* @return a Range instance
1617
*/
1718
public static Range of(int lowerBound, int upperBound) {
1819
return new Range(lowerBound, upperBound);
@@ -25,7 +26,8 @@ private Range(int lowerBound, int upperBound) {
2526

2627
/**
2728
* Checks if the passed httpStatusCode is contained in given range
28-
* @param httpStatusCode
29+
*
30+
* @param httpStatusCode the status code to check
2931
* @return true if contains, otherwise false
3032
*/
3133
public boolean contains(int httpStatusCode) {

errorhandler-matchers/retrofit-rx-matcher/src/main/java/com/workable/errorhandler/matchers/retrofit/RetrofitMatcherFactory.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ private RetrofitMatcherFactory() {
1414

1515
/**
1616
* Creates a {@link MatcherFactory} that checks HTTP statuses
17-
* @return
17+
*
18+
* @return new MatcherFactory for Retrofit Rx HttpException that works with Integer
1819
*/
1920
public static MatcherFactory<Integer> create() {
2021
return new MatcherFactory<Integer>() {
@@ -31,7 +32,8 @@ public boolean matches(Throwable throwable) {
3132

3233
/**
3334
* Creates a {@link MatcherFactory} that checks if HTTP status is in given {@link Range}
34-
* @return
35+
*
36+
* @return new MatcherFactory for Retrofit Rx HttpException that works with Range
3537
*/
3638
public static MatcherFactory<Range> createRange() {
3739
return new MatcherFactory<Range>() {

errorhandler-matchers/retrofit-rx-matcher/src/test/java/com/workable/errorhandler/matchers/retrofit/RetrofitMatcherFactoryTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ protected void setUp() throws Exception {
2727
public void test_catching_exact_http_code() {
2828
ErrorHandler
2929
.createIsolated()
30-
.bindErrorCode(400, RetrofitMatcherFactory.create())
31-
.bindErrorCodeClass(Range.class, RetrofitMatcherFactory.createRange())
30+
.bind(400, RetrofitMatcherFactory.create())
31+
.bindClass(Range.class, RetrofitMatcherFactory.createRange())
3232
.on(400, new Action() {
3333
@Override
3434
public void execute(Throwable throwable, ErrorHandler errorHandler) {
@@ -51,8 +51,8 @@ public void execute(Throwable throwable, ErrorHandler errorHandler) {
5151
public void test_not_catching_exact_http_code() {
5252
ErrorHandler
5353
.createIsolated()
54-
.bindErrorCode(400, RetrofitMatcherFactory.create())
55-
.bindErrorCodeClass(Range.class, RetrofitMatcherFactory.createRange())
54+
.bind(400, RetrofitMatcherFactory.create())
55+
.bindClass(Range.class, RetrofitMatcherFactory.createRange())
5656
.on(400, new Action() {
5757
@Override
5858
public void execute(Throwable throwable, ErrorHandler errorHandler) {
@@ -75,7 +75,7 @@ public void execute(Throwable throwable, ErrorHandler errorHandler) {
7575
public void test_catching_with_class() {
7676
ErrorHandler
7777
.createIsolated()
78-
.bindErrorCodeClass(Integer.class, RetrofitMatcherFactory.create())
78+
.bindClass(Integer.class, RetrofitMatcherFactory.create())
7979
.on(500, new Action() {
8080
@Override
8181
public void execute(Throwable throwable, ErrorHandler errorHandler) {

errorhandler/build.gradle

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ plugins {
99
}
1010

1111
group 'com.workable'
12-
version '0.9.1'
12+
version '1.0.0'
1313

1414
apply plugin: 'java'
1515
apply plugin: 'maven-publish'
@@ -36,18 +36,27 @@ task sourceJar(type: Jar) {
3636
from sourceSets.main.allJava
3737
}
3838

39+
task javadocJar(type: Jar) {
40+
classifier = 'javadoc'
41+
from javadoc
42+
}
43+
3944
publishing {
4045
publications {
4146
mavenJava(MavenPublication) {
4247
groupId 'com.workable'
4348
artifactId 'error-handler'
44-
version '0.9.1'
49+
version '1.0.0'
4550

4651
from components.java
4752

4853
artifact sourceJar {
4954
classifier "sources"
5055
}
56+
57+
artifact javadocJar {
58+
classifier "javadoc"
59+
}
5160
}
5261
}
5362

@@ -81,10 +90,10 @@ bintray {
8190
labels = ['java', 'error handler', 'errors', 'android']
8291
publicDownloadNumbers = true
8392
version {
84-
name = '0.9.1'
93+
name = '1.0.0'
8594

8695
desc = 'Error handling library for Android and Java'
87-
vcsTag = '0.9.1'
96+
vcsTag = 'v1.0.0'
8897
gpg {
8998
sign = true //Determines whether to GPG sign the files. The default is false
9099
}

errorhandler/src/main/java/com/workable/errorhandler/Action.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* The MIT License
33
*
4-
* Copyright (c) 2010-2016 Workable SA
4+
* Copyright (c) 2013-2016 Workable SA
55
*
66
* Permission is hereby granted, free of charge, to any person obtaining a copy
77
* of this software and associated documentation files (the "Software"), to deal

errorhandler/src/main/java/com/workable/errorhandler/ActionEntry.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* The MIT License
33
*
4-
* Copyright (c) 2010-2016 Workable SA
4+
* Copyright (c) 2013-2016 Workable SA
55
*
66
* Permission is hereby granted, free of charge, to any person obtaining a copy
77
* of this software and associated documentation files (the "Software"), to deal
@@ -71,6 +71,7 @@ public int hashCode() {
7171
*
7272
* @param matcher the matcher object in the ActionEntry
7373
* @param action the action object in the ActionEntry
74+
* @return a new ActionEntry
7475
*/
7576
public static ActionEntry from(Matcher matcher, Action action) {
7677
return new ActionEntry(matcher, action);

0 commit comments

Comments
 (0)