Skip to content

Commit 4aaad99

Browse files
committed
Documentation on README.md
1 parent 3c503bc commit 4aaad99

1 file changed

Lines changed: 58 additions & 11 deletions

File tree

README.md

Lines changed: 58 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
[![Build Status](https://travis-ci.org/wavesoftware/java-eid-exceptions.svg?branch=master)](https://travis-ci.org/wavesoftware/java-eid-exceptions) [![Coverage Status](https://coveralls.io/repos/wavesoftware/java-eid-exceptions/badge.svg?branch=master&service=github)](https://coveralls.io/github/wavesoftware/java-eid-exceptions?branch=master) [![SonarQube Tech Debt](https://img.shields.io/sonar/http/sonar-ro.wavesoftware.pl/pl.wavesoftware:eid-exceptions/tech_debt.svg)](http://sonar-ro.wavesoftware.pl/dashboard/index/2600) [![Dependency Status](https://www.versioneye.com/user/projects/55aafc74306535001b000440/badge.svg?style=flat)](https://www.versioneye.com/user/projects/55aafc74306535001b000440) [![Maven Central](https://img.shields.io/maven-central/v/pl.wavesoftware/eid-exceptions.svg)](http://search.maven.org/#search%7Cga%7C1%7Cg%3A%22pl.wavesoftware%22%20AND%20a%3A%22eid-exceptions%22)
44

5-
This small library holds a set of Exceptions that implements idea of fast, reusable, error codes that can be simple thrown fast in case of unpredictable and unrecoverable application failure.
5+
This small library holds a set of exceptions and utilities that implements idea of fast, reusable, error codes that can be simply thrown fast in case of unpredictable and unrecoverable application failure. It is meant to be used for application bugs.
66

77
## Idea
88

9-
The idea is to use a set of simple runtime exceptions. They should always take the field Exception ID (Eid) in the making. This field will then be reported when displaying or logging that exception. It can also be viewed on the professional fatal error window of the application as a bug reference. EidRuntimeExceptions contains also additional unique ID to identify each single exception. This approach simplifies the management of exceptions in the application and allows developers to focus on functionalities rather than coming up with the correct statement for the exception.
9+
The idea is to use a set of simple runtime exceptions. They should always take the Exception ID (Eid) object in the making. This eid object will then be reported when displaying or logging that exception. It can also be viewed on the professional fatal error window of the application as a bug reference. EidRuntimeExceptions contains also additional unique ID to distinguish each single exception from others with same Eid. This approach simplifies the management of exceptions in the application and allows developers to focus on functionalities rather than coming up with the correct statement for the exception.
1010

1111
This approach is best to use with tools and plugins like:
1212

@@ -43,17 +43,17 @@ This classes shouldn't be used in any public API or library. It is designed to b
4343
<dependency>
4444
<groupId>pl.wavesoftware</groupId>
4545
<artifactId>eid-exceptions</artifactId>
46-
<version>1.0.0</version>
46+
<version>1.1.0</version>
4747
</dependency>
4848
```
4949

5050
### `EidPreconditions` class
5151

5252
#### General use
5353

54-
Static convenience methods that help a method or constructor check whether it was invoked correctly (whether its preconditions have been met). These methods generally accept a `boolean` expression which is expected to be `true` (or in the case of `checkNotNull`, an object reference which is expected to be non-null). When `false` (or `null`) is passed instead, the `EidPreconditions` method throws an unchecked exception, which helps the calling method communicate to its caller that that caller has made a mistake.
54+
`EidPreconditions` class consists static methods that help to use Eid in a method or constructor. This is solely for convenience purposes. Use them to check whether method or constructor was invoked correctly (whether its preconditions have been met). These methods generally accept a `boolean` expression which is expected to be `true` (or in the case of `checkNotNull`, an object reference which is expected to be non-null). When `false` (or `null`) is passed instead, the `EidPreconditions` method throws an unchecked exception, which helps the calling method communicate to its caller that that caller has made a mistake.
5555

56-
Each method accepts a EID string or Eid object, which is designed to ease of use and provide strict ID for given exception usage. This approach speed up development of large application and helps support teams by giving both static and random ID for each possible unpredicted bug.
56+
Each method accepts a EID string or Eid object, which is designed to ease of use and provide strict ID for given exception usage. This approach speed up development of large application and helps support teams by giving both static and random ID for each possible bug that could occur.
5757

5858
Each example uses static import:
5959

@@ -104,23 +104,66 @@ String nonNullUserName = checkNotNull(userName, "20150721:115515");
104104
```java
105105
checkElementIndex(index, list.size(), "20150721:115749");
106106
```
107+
108+
#### Formatted message support
109+
110+
From release `1.1.0` there have been added methods to support additional formatted messages for `checkArgument`, `checkState`, `checkNotNull` and `checkElementIndex` method. Those method versions can sometimes be used to pass additional information to exceptions that will be displayed in log files.
111+
112+
Message formatting is done using `String.format(String, Object[])` method.
113+
114+
For example:
115+
116+
```java
117+
checkState(transation.isValid(), "20151119:120238", "Invalid transaction: %s, transaction);
118+
```
119+
120+
Will produce output similar to;
121+
122+
```
123+
pl.wavesoftware.eid.exceptions.EidIllegalStateException: [20151119:120238]<xf4j1l> => Invalid transaction: <Transaction id=null, buyer=null, products=[]>
124+
```
107125
108126
#### Functional try to execute blocks
109127
110-
Using functional blocks to handle operations, that are intended to operate properly, simplify the code and makes it more readable. It's also good way to deal with untested, uncovered `catch` blocks. It's easy and gives developers nice way of dealing with countless operations that suppose to work as intended.
128+
You can use functional blocks to handle operations, that are intended to operate properly. This approach simplify the code and makes it more readable. It's also good way to deal with untested, uncovered `catch` blocks. It's easy and gives developers nice way of dealing with countless operations that suppose to work as intended.
129+
130+
There are two versions. One with `UnsafeSupplier` and one with `UnsafeProcedure`. The difference is that unsafe procedure do not return anything.
111131
112132
Example:
113133
114134
```java
115-
InputStream is = EidPreconditions.tryToExecute(new RiskyCode<InputStream>() {
135+
InputStream is = EidPreconditions.tryToExecute(new UnsafeSupplier<InputStream>() {
116136
@Override
117-
public InputStream execute() throws IOException {
137+
public InputStream get() throws IOException {
118138
return this.getClass().getClassLoader()
119139
.getResourceAsStream("project.properties");
120140
}
121141
}, "20150718:121521");
122142
```
123143
144+
or with Java 8:
145+
146+
```java
147+
import static pl.wavesoftware.eid.utils.EidPreconditions.tryToExecute;
148+
// [..]
149+
InputStream is = tryToExecute(() -> { resource("project.properties"); }, "20150718:121521");
150+
```
151+
152+
#### Logging
153+
154+
Eid object can also be useful in logging. That are `makeLogMessage` method provided to do that. Message formatting is done using `String.format(String, Object[])` method.
155+
For example:
156+
157+
```java
158+
log.debug(new Eid("20151119:121814").makeLogMessage("REST request received: %s", request));
159+
```
160+
161+
will unfold to something similar to:
162+
163+
```
164+
2017-01-08T16:45:34,334 DEBUG [a.b.c.RestBroker] [20151119:121814]<d1afca> REST request received: <RestRequest user=<User id=345> flow=ShowLastTransactions step=Confirm>
165+
```
166+
124167
###Contributing
125168
126169
Contributions are welcome!
@@ -141,8 +184,12 @@ Even if you can't contribute code, if you have an idea for an improvement please
141184
142185
### Releases
143186
187+
- 1.1.0
188+
- Adding support for formatted messages in exceptions and also in utility methods of `EidPreconditions`
189+
- 1.0.1
190+
- Fixed handling for throwables as a cause with `message == null`. `cause.toString()` method is used
144191
- 1.0.0
145-
- Support for JDK >= 1.6
192+
- Support for JDK >= 1.6
146193
- 0.1.0
147-
- initial release
148-
- idea imported from Guava Library and COI code
194+
- initial release
195+
- idea imported from Guava Library and COI code

0 commit comments

Comments
 (0)