Skip to content

Commit d010a01

Browse files
author
Taha BASRI
committed
Pre-release 0.1
- fixed javadoc errors - fixed javadoc and sources plugins - added documentation about custom messages
1 parent d8ac3cd commit d010a01

5 files changed

Lines changed: 137 additions & 33 deletions

File tree

README.md

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,86 @@ class Spaceship{
105105

106106
When mixing annotation and programmatic API, take-a-hint will opt for configuration by programmatic API.
107107

108+
### Provide easy hints for final users
109+
110+
It's much better when the final user can get hints on how to fix errors at failure time. take-a-hint offers custom Exception classes to help you communicate hints easily.
111+
112+
If you have already a code block that throws an exception, you can wrap it inside `HintException` or `HintRuntimeException`. This will let you personalize final error messages depending on your needs. Here are some examples:
113+
114+
```java
115+
class Spaceship {
116+
private void goToMars() {
117+
throw HintRuntimeException.of(
118+
new IllegalStateException("Oxygen leak !!!"), // this is your regular exception
119+
"Check your equipments !" // you can set a custom hint message to be shown to final user
120+
);
121+
}
122+
}
123+
```
124+
125+
When the error gets thrown, the final user will get the following message (depending on configuration):
126+
127+
```
128+
❌ error: Application failed with exception : java.lang.IllegalStateException: Oxygen leak !!!
129+
130+
✅ hints: Check your equipments !
131+
```
132+
133+
You can work with checked exceptions while using take-a-hint via the custom exception class `HintException` :
134+
135+
```java
136+
class Spaceship {
137+
private void goToMars() throws HintException {
138+
throw HintException.of(new IllegalStateException("Oxygen leak !!!"), "Check your equipments !");
139+
}
140+
}
141+
// ...
142+
try {
143+
new Spaceship().goToMars();
144+
} catch (HintException ex) { // HintException is a checked exception, should be thrown
145+
System.out.println("Custom error : " + ex.getHintsMsg()); // use exception instance as you want
146+
}
147+
148+
```
149+
150+
Another cool thing you can do with take-a-hint is set a global hint message for method or class. Let's say you have a method that may throw an exception in multiple occasions, and you want to provide a single hint message for the whole method. Then, you can use the annotation `@HintMessage` to do that.
151+
152+
```java
153+
@Hint
154+
class Spaceship {
155+
@HintMessage("Check your equipments")
156+
private void goToMars(int x) {
157+
if(x==-1){
158+
throw new IllegalStateException("Crash !");
159+
}else if(x==0){
160+
throw new IllegalStateException("Boom !");
161+
}else{
162+
// go
163+
}
164+
}
165+
}
166+
```
167+
168+
For each exception thrown within the method `goToMars`, take-a-hint will display (depending on the configuration) the custom hint message provided by the annotation `@HintMessage`.
169+
170+
You can use the same annotation with the parent class. In that case, the provided message in the class will be shown whenever an exception gets thrown within a method of that class. Each method with its own annotation will override the class custom message.
171+
172+
```java
173+
@Hint
174+
@HintMessage("Check your equipments !")
175+
class Spaceship {
176+
@HintMessage("What about heat ?") // this message will override the one in parent class
177+
private void goToTheSun(int x) {
178+
throw new IllegalStateException("Burned !");
179+
}
180+
181+
// will use parent message
182+
private void goToMars() {
183+
throw new IllegalStateException("Boom !");
184+
}
185+
}
186+
```
187+
108188
### Use with Picocli
109189

110190
[Picocli](https://picocli.info/) is a one-file framework for creating Java command line applications with almost zero code.

pom.xml

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -50,39 +50,41 @@
5050
</dependencies>
5151

5252
<build>
53+
<plugins>
54+
<plugin>
55+
<groupId>org.apache.maven.plugins</groupId>
56+
<artifactId>maven-source-plugin</artifactId>
57+
<version>3.2.0</version>
58+
<executions>
59+
<execution>
60+
<id>attach-sources</id>
61+
<goals>
62+
<goal>jar</goal>
63+
</goals>
64+
</execution>
65+
</executions>
66+
</plugin>
67+
<plugin>
68+
<groupId>org.apache.maven.plugins</groupId>
69+
<artifactId>maven-javadoc-plugin</artifactId>
70+
<version>3.2.0</version>
71+
<executions>
72+
<execution>
73+
<id>attach-javadocs</id>
74+
<goals>
75+
<goal>jar</goal>
76+
</goals>
77+
</execution>
78+
</executions>
79+
</plugin>
80+
<plugin>
81+
<groupId>org.apache.maven.plugins</groupId>
82+
<artifactId>maven-surefire-plugin</artifactId>
83+
<version>3.0.0-M4</version>
84+
</plugin>
85+
</plugins>
5386
<pluginManagement>
5487
<plugins>
55-
<plugin>
56-
<groupId>org.apache.maven.plugins</groupId>
57-
<artifactId>maven-source-plugin</artifactId>
58-
<version>3.2.0</version>
59-
<executions>
60-
<execution>
61-
<id>attach-sources</id>
62-
<goals>
63-
<goal>jar</goal>
64-
</goals>
65-
</execution>
66-
</executions>
67-
</plugin>
68-
<plugin>
69-
<groupId>org.apache.maven.plugins</groupId>
70-
<artifactId>maven-javadoc-plugin</artifactId>
71-
<version>3.2.0</version>
72-
<executions>
73-
<execution>
74-
<id>attach-javadocs</id>
75-
<goals>
76-
<goal>jar</goal>
77-
</goals>
78-
</execution>
79-
</executions>
80-
</plugin>
81-
<plugin>
82-
<groupId>org.apache.maven.plugins</groupId>
83-
<artifactId>maven-surefire-plugin</artifactId>
84-
<version>3.0.0-M4</version>
85-
</plugin>
8688
<plugin>
8789
<groupId>org.codehaus.mojo</groupId>
8890
<artifactId>cobertura-maven-plugin</artifactId>
@@ -92,7 +94,7 @@
9294
<format>html</format>
9395
<format>xml</format>
9496
</formats>
95-
<check />
97+
<check/>
9698
</configuration>
9799
</plugin>
98100
</plugins>

src/main/java/io/hint/HintCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
*
2828
* <p>You can configure your instance using annotation {@link Hint} or programmatic API (static methods)</p>
2929
* <p><b>Note : </b>Programmatic API overrides annotation configuration</p>
30-
* <br/>
30+
*
3131
* <p>If no explicit configuration is provided,
3232
* the new instance uses the default configuration specified with {@link Hint} properties</p>.
3333
*/

src/main/java/io/hint/annotation/Hint.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,28 +39,38 @@
3939

4040
/**
4141
* Shows or hides stacktrace in final output
42+
*
43+
* @return show stacktrace
4244
*/
4345
boolean showStackTrace() default false;
4446

4547
/**
4648
* Shows or hides hints messages in final output
49+
*
50+
* @return hints
4751
*/
4852
boolean showHints() default true;
4953

5054
// default messages
5155

5256
/**
5357
* Sets default message for exceptions without custom error message
58+
*
59+
* @return default exception message
5460
*/
5561
String defaultExceptionMessage() default "Application failed with exception : ";
5662

5763
/**
5864
* Sets default message for notes about documentations
65+
*
66+
* @return default docs message
5967
*/
6068
String defaultDocsMessage() default "See the docs for details : ";
6169

6270
/**
6371
* Sets default exit code to be used by your program when an uncaught exception gets thrown
72+
*
73+
* @return default exit code
6474
*/
6575
int defaultExitCode() default 1;
6676

@@ -69,24 +79,28 @@
6979
/**
7080
* Sets default prefix to be used for each line in hints messages
7181
*
82+
* @return hint prefix
7283
*/
7384
String hintPrefix() default "\u2705 hints:";
7485

7586
/**
7687
* Sets default prefix to be used for each line in error messages
7788
*
89+
* @return error prefix
7890
*/
7991
String errorPrefix() default "\u274C error:";
8092

8193
/**
8294
* Sets default prefix to be used for each line in stacktrace
8395
*
96+
* @return stack prefix
8497
*/
8598
String stackPrefix() default "\u26D4 stack:";
8699

87100
/**
88101
* Sets default prefix to be used for each line in usage messages (docs)
89102
*
103+
* @return docs prefix
90104
*/
91105
String docsPrefix() default "\u2754 usage:";
92106

@@ -100,16 +114,22 @@
100114
* ❔ usage: ---
101115
* ❔ usage: See the docs for details : URL
102116
* </pre>
117+
*
118+
* @return default docs separator
103119
*/
104120
String defaultDocsSeparator() default "---";
105121

106122
/**
107123
* Sets default separator to be used between each token in final output
124+
*
125+
* @return default separator
108126
*/
109127
String defaultSeparator() default "\t";
110128

111129
/**
112130
* Sets your global documentation url, if unset, documentation help message won't show up on your final output
131+
*
132+
* @return docs URL
113133
*/
114134
// misc
115135
String docsUrl() default "";

src/main/java/io/hint/annotation/HintMessage.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@
5555
public @interface HintMessage {
5656
/**
5757
* Sets default message to show for each exception thrown at the annotated element.
58+
*
59+
* @return default exception message
5860
*/
5961
String value() default "";
6062
}

0 commit comments

Comments
 (0)