Skip to content

Commit 6f70d53

Browse files
committed
Add documentation for FailureAnalyzer
Closes gh-6775
1 parent 0e00a49 commit 6f70d53

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

spring-boot-docs/src/main/asciidoc/howto.adoc

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,30 @@ can send us a {github-code}[pull request].
2222
== Spring Boot application
2323

2424

25+
[[howto-failure-analyzer]]
26+
=== Create your own FailureAnalyzer
27+
{dc-spring-boot}/diagnostics/FailureAnalyzer.{dc-ext}[[`FailureAnalyzer`] is a great way
28+
to intercept an exception on startup and turn it into a human-readable message, wrapped
29+
into a {dc-spring-boot}/diagnostics/FailureAnalysis.{dc-ext}[[`FailureAnalysis`]. Spring
30+
Boot provides such analyzer for application context related exceptions, JSR-303
31+
validations and more. It is actually very easy to create your own.
32+
33+
`AbstractFailureAnalyzer` is a convenient extension of `FailureAnalyzer` that checks the
34+
presence of a specified exception type in the exception to handle. You can extend from
35+
that so that your implementation gets a chance to handle the exception only when it is
36+
actually present. If for whatever reason you can't handle the exception, return `null`
37+
to let another implementation a chance to handle the exception.
38+
39+
`FailureAnalyzer` implementations are to be registered in a `META-INF/spring.factories`:
40+
the following registers `ProjectConstraintViolationFailureAnalyzer`:
41+
42+
[source,properties,indent=0]
43+
----
44+
org.springframework.boot.diagnostics.FailureAnalyzer=\
45+
com.example.ProjectConstraintViolationFailureAnalyzer
46+
----
47+
48+
2549

2650
[[howto-troubleshoot-auto-configuration]]
2751
=== Troubleshoot auto-configuration

spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,29 @@ When your application starts you should see something similar to the following:
4646
By default `INFO` logging messages will be shown, including some relevant startup details
4747
such as the user that launched the application.
4848

49+
If your application fails to start, registered `FailureAnalyzers` get a chance to provide
50+
a dedicated error message and a concrete action to fix the problem. For instance if you
51+
start a web application on port `8080` and that port is already in use, you should see
52+
something similar to the following:
53+
54+
[indent=0]
55+
----
56+
***************************
57+
APPLICATION FAILED TO START
58+
***************************
59+
60+
Description:
61+
62+
Embedded servlet container failed to start. Port 8080 was already in use.
63+
64+
Action:
65+
66+
Identify and stop the process that's listening on port 8080 or configure this application to listen on another port.
67+
----
68+
69+
NOTE: Spring Boot provides numerous `FailureAnalyzer` implementations and you can
70+
<<howto.adoc#howto-failure-analyzer,add your own>> very easily.
71+
4972

5073
[[boot-features-banner]]
5174
=== Customizing the Banner

0 commit comments

Comments
 (0)