Skip to content

Commit 26a6cc2

Browse files
committed
Add utility functions for unwrapping R2DBC exceptions in ExposedR2dbcException
1 parent 943a529 commit 26a6cc2

1 file changed

Lines changed: 155 additions & 0 deletions

File tree

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
package dev.slne.surf.database.utils
2+
3+
import io.r2dbc.spi.*
4+
import org.jetbrains.exposed.v1.r2dbc.ExposedR2dbcException
5+
6+
/**
7+
* Unwraps the underlying R2DBC exception from this ExposedR2dbcException.
8+
*
9+
* @return the underlying R2DBC exception cause
10+
* @throws ExposedR2dbcException if the cause is not an R2dbcException
11+
*/
12+
fun ExposedR2dbcException.unwrap(): R2dbcException {
13+
return cause as? R2dbcException ?: throw this
14+
}
15+
16+
/**
17+
* Unwraps the underlying R2DBC exception and casts it to the specified type.
18+
*
19+
* @param E the expected exception type
20+
* @return the underlying exception cast to type E
21+
* @throws ExposedR2dbcException if the cause cannot be cast to type E
22+
*/
23+
@JvmName($$"unwrap$reified")
24+
inline fun <reified E : R2dbcException> ExposedR2dbcException.unwrap(): E {
25+
return cause as? E ?: throw this
26+
}
27+
28+
/**
29+
* Alias for [unwrap]. Unwraps and casts the underlying exception to the specified type.
30+
*
31+
* @param E the expected exception type
32+
* @return the underlying exception cast to type E
33+
* @throws ExposedR2dbcException if the cause cannot be cast to type E
34+
* @see unwrap
35+
*/
36+
inline fun <reified E : R2dbcException> ExposedR2dbcException.causeAs(): E = unwrap<E>()
37+
38+
/**
39+
* Checks if the underlying cause is a data integrity violation.
40+
*
41+
* @return true if the cause is a data integrity violation, false otherwise
42+
* @see R2dbcDataIntegrityViolationException
43+
*/
44+
fun ExposedR2dbcException.isDataIntegrityViolation() = cause is R2dbcDataIntegrityViolationException
45+
46+
/**
47+
* Unwraps and returns the underlying exception as a data integrity violation.
48+
*
49+
* @return the underlying data integrity violation exception
50+
* @throws ExposedR2dbcException if the cause is not a data integrity violation
51+
* @see R2dbcDataIntegrityViolationException
52+
*/
53+
fun ExposedR2dbcException.asDataIntegrityViolation() = unwrap<R2dbcDataIntegrityViolationException>()
54+
55+
/**
56+
* Checks if the underlying cause is a bad grammar exception.
57+
*
58+
* @return true if the cause is a bad grammar exception, false otherwise
59+
* @see R2dbcBadGrammarException
60+
*/
61+
fun ExposedR2dbcException.isBadGrammar() = cause is R2dbcBadGrammarException
62+
63+
/**
64+
* Unwraps and returns the underlying exception as a bad grammar exception.
65+
*
66+
* @return the underlying bad grammar exception
67+
* @throws ExposedR2dbcException if the cause is not a bad grammar exception
68+
* @see R2dbcBadGrammarException
69+
*/
70+
fun ExposedR2dbcException.asBadGrammar() = unwrap<R2dbcBadGrammarException>()
71+
72+
/**
73+
* Checks if the underlying cause is a non-transient resource exception.
74+
*
75+
* @return true if the cause is a non-transient resource exception, false otherwise
76+
* @see R2dbcNonTransientResourceException
77+
*/
78+
fun ExposedR2dbcException.isNonTransientResourceException() = cause is R2dbcNonTransientResourceException
79+
80+
/**
81+
* Unwraps and returns the underlying exception as a non-transient resource exception.
82+
*
83+
* @return the underlying non-transient resource exception
84+
* @throws ExposedR2dbcException if the cause is not a non-transient resource exception
85+
* @see R2dbcNonTransientResourceException
86+
*/
87+
fun ExposedR2dbcException.asNonTransientResourceException() = unwrap<R2dbcNonTransientResourceException>()
88+
89+
/**
90+
* Checks if the underlying cause is a permission denied exception.
91+
*
92+
* @return true if the cause is a permission denied exception, false otherwise
93+
* @see R2dbcPermissionDeniedException
94+
*/
95+
fun ExposedR2dbcException.isPermissionDenied() = cause is R2dbcPermissionDeniedException
96+
97+
/**
98+
* Unwraps and returns the underlying exception as a permission denied exception.
99+
*
100+
* @return the underlying permission denied exception
101+
* @throws ExposedR2dbcException if the cause is not a permission denied exception
102+
* @see R2dbcPermissionDeniedException
103+
*/
104+
fun ExposedR2dbcException.asPermissionDenied() = unwrap<R2dbcPermissionDeniedException>()
105+
106+
/**
107+
* Checks if the underlying cause is a rollback exception.
108+
*
109+
* @return true if the cause is a rollback exception, false otherwise
110+
* @see R2dbcRollbackException
111+
*/
112+
fun ExposedR2dbcException.isRollbackException() = cause is R2dbcRollbackException
113+
114+
/**
115+
* Unwraps and returns the underlying exception as a rollback exception.
116+
*
117+
* @return the underlying rollback exception
118+
* @throws ExposedR2dbcException if the cause is not a rollback exception
119+
* @see R2dbcRollbackException
120+
*/
121+
fun ExposedR2dbcException.asRollbackException() = unwrap<R2dbcRollbackException>()
122+
123+
/**
124+
* Checks if the underlying cause is a timeout exception.
125+
*
126+
* @return true if the cause is a timeout exception, false otherwise
127+
* @see R2dbcTimeoutException
128+
*/
129+
fun ExposedR2dbcException.isTimeoutException() = cause is R2dbcTimeoutException
130+
131+
/**
132+
* Unwraps and returns the underlying exception as a timeout exception.
133+
*
134+
* @return the underlying timeout exception
135+
* @throws ExposedR2dbcException if the cause is not a timeout exception
136+
* @see R2dbcTimeoutException
137+
*/
138+
fun ExposedR2dbcException.asTimeoutException() = unwrap<R2dbcTimeoutException>()
139+
140+
/**
141+
* Checks if the underlying cause is a transient resource exception.
142+
*
143+
* @return true if the cause is a transient resource exception, false otherwise
144+
* @see R2dbcTransientResourceException
145+
*/
146+
fun ExposedR2dbcException.isTransientResourceException() = cause is R2dbcTransientResourceException
147+
148+
/**
149+
* Unwraps and returns the underlying exception as a transient resource exception.
150+
*
151+
* @return the underlying transient resource exception
152+
* @throws ExposedR2dbcException if the cause is not a transient resource exception
153+
* @see R2dbcTransientResourceException
154+
*/
155+
fun ExposedR2dbcException.asTransientResourceException() = unwrap<R2dbcTransientResourceException>()

0 commit comments

Comments
 (0)