diff options
author | Robert Jaros <rjaros@finn.pl> | 2019-11-01 17:44:31 +0100 |
---|---|---|
committer | Robert Jaros <rjaros@finn.pl> | 2019-11-01 17:44:31 +0100 |
commit | 5763aa3b0004b678d7791b32e1a92b6facb9b17e (patch) | |
tree | 3bd6ca99f2f2dbec9e196ca63215c7ba4bce6603 | |
parent | c4a43b9c8ce6db65784e9bcb7073dcf6c4a6cd41 (diff) | |
download | kvision-5763aa3b0004b678d7791b32e1a92b6facb9b17e.tar.gz kvision-5763aa3b0004b678d7791b32e1a92b6facb9b17e.tar.bz2 kvision-5763aa3b0004b678d7791b32e1a92b6facb9b17e.zip |
Better exception handling in server-side interfaces.
6 files changed, 82 insertions, 49 deletions
diff --git a/kvision-modules/kvision-common-remote/src/main/kotlin/pl/treksoft/kvision/remote/JsonRpc.kt b/kvision-modules/kvision-common-remote/src/main/kotlin/pl/treksoft/kvision/remote/JsonRpc.kt index 7953ea01..d82189a0 100644 --- a/kvision-modules/kvision-common-remote/src/main/kotlin/pl/treksoft/kvision/remote/JsonRpc.kt +++ b/kvision-modules/kvision-common-remote/src/main/kotlin/pl/treksoft/kvision/remote/JsonRpc.kt @@ -33,5 +33,6 @@ data class JsonRpcResponse( val id: Int? = null, val result: String? = null, val error: String? = null, + val exceptionType: String? = null, val jsonrpc: String = "2.0" ) diff --git a/kvision-modules/kvision-common-remote/src/main/kotlin/pl/treksoft/kvision/remote/RemoteTypes.kt b/kvision-modules/kvision-common-remote/src/main/kotlin/pl/treksoft/kvision/remote/RemoteTypes.kt index 29cb6a9b..62b2354b 100644 --- a/kvision-modules/kvision-common-remote/src/main/kotlin/pl/treksoft/kvision/remote/RemoteTypes.kt +++ b/kvision-modules/kvision-common-remote/src/main/kotlin/pl/treksoft/kvision/remote/RemoteTypes.kt @@ -23,6 +23,8 @@ package pl.treksoft.kvision.remote import kotlinx.serialization.Serializable +class ServiceException(message: String) : Exception(message) + @Serializable data class RemoteOption( val value: String? = null, diff --git a/kvision-modules/kvision-remote/src/main/kotlin/pl/treksoft/kvision/remote/CallAgent.kt b/kvision-modules/kvision-remote/src/main/kotlin/pl/treksoft/kvision/remote/CallAgent.kt index 64c257cd..0a56465d 100644 --- a/kvision-modules/kvision-remote/src/main/kotlin/pl/treksoft/kvision/remote/CallAgent.kt +++ b/kvision-modules/kvision-remote/src/main/kotlin/pl/treksoft/kvision/remote/CallAgent.kt @@ -71,7 +71,13 @@ open class CallAgent { { data: dynamic, _: Any, _: Any -> when { data.id != jsonRpcRequest.id -> reject(Exception("Invalid response ID")) - data.error != null -> reject(Exception(data.error.toString())) + data.error != null -> { + if (data.exceptionType == "pl.treksoft.kvision.remote.ServiceException") { + reject(ServiceException(data.error.toString())) + } else { + reject(Exception(data.error.toString())) + } + } data.result != null -> resolve(data.result) else -> reject(Exception("Invalid response")) } diff --git a/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt b/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt index bb682351..d81dc7a7 100644 --- a/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt +++ b/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt @@ -106,8 +106,9 @@ actual open class KVServiceManager<T : Any> actual constructor(val serviceClass: ) ) } catch (e: Exception) { - LOG.error(e.message, e) - res.send(JsonRpcResponse(id = jsonRpcRequest.id, error = e.message ?: "Error")) + if (!(e is ServiceException)) LOG.error(e.message, e) + res.send(JsonRpcResponse(id = jsonRpcRequest.id, error = e.message ?: "Error", + exceptionType = e.javaClass.canonicalName)) } } }.invoke(this) @@ -145,8 +146,9 @@ actual open class KVServiceManager<T : Any> actual constructor(val serviceClass: ) ) } catch (e: Exception) { - LOG.error(e.message, e) - res.send(JsonRpcResponse(id = jsonRpcRequest.id, error = e.message ?: "Error")) + if (!(e is ServiceException)) LOG.error(e.message, e) + res.send(JsonRpcResponse(id = jsonRpcRequest.id, error = e.message ?: "Error", + exceptionType = e.javaClass.canonicalName)) } } } else { @@ -188,8 +190,9 @@ actual open class KVServiceManager<T : Any> actual constructor(val serviceClass: ) ) } catch (e: Exception) { - LOG.error(e.message, e) - res.send(JsonRpcResponse(id = jsonRpcRequest.id, error = e.message ?: "Error")) + if (!(e is ServiceException)) LOG.error(e.message, e) + res.send(JsonRpcResponse(id = jsonRpcRequest.id, error = e.message ?: "Error", + exceptionType = e.javaClass.canonicalName)) } } } else { @@ -233,8 +236,9 @@ actual open class KVServiceManager<T : Any> actual constructor(val serviceClass: ) ) } catch (e: Exception) { - LOG.error(e.message, e) - res.send(JsonRpcResponse(id = jsonRpcRequest.id, error = e.message ?: "Error")) + if (!(e is ServiceException)) LOG.error(e.message, e) + res.send(JsonRpcResponse(id = jsonRpcRequest.id, error = e.message ?: "Error", + exceptionType = e.javaClass.canonicalName)) } } } else { @@ -279,8 +283,9 @@ actual open class KVServiceManager<T : Any> actual constructor(val serviceClass: ) ) } catch (e: Exception) { - LOG.error(e.message, e) - res.send(JsonRpcResponse(id = jsonRpcRequest.id, error = e.message ?: "Error")) + if (!(e is ServiceException)) LOG.error(e.message, e) + res.send(JsonRpcResponse(id = jsonRpcRequest.id, error = e.message ?: "Error", + exceptionType = e.javaClass.canonicalName)) } } } else { @@ -327,8 +332,9 @@ actual open class KVServiceManager<T : Any> actual constructor(val serviceClass: ) ) } catch (e: Exception) { - LOG.error(e.message, e) - res.send(JsonRpcResponse(id = jsonRpcRequest.id, error = e.message ?: "Error")) + if (!(e is ServiceException)) LOG.error(e.message, e) + res.send(JsonRpcResponse(id = jsonRpcRequest.id, error = e.message ?: "Error", + exceptionType = e.javaClass.canonicalName)) } } } else { @@ -440,8 +446,9 @@ actual open class KVServiceManager<T : Any> actual constructor(val serviceClass: ) ) } catch (e: Exception) { - LOG.error(e.message, e) - res.send(JsonRpcResponse(id = jsonRpcRequest.id, error = e.message ?: "Error")) + if (!(e is ServiceException)) LOG.error(e.message, e) + res.send(JsonRpcResponse(id = jsonRpcRequest.id, error = e.message ?: "Error", + exceptionType = e.javaClass.canonicalName)) } } } else { @@ -482,8 +489,9 @@ actual open class KVServiceManager<T : Any> actual constructor(val serviceClass: ) ) } catch (e: Exception) { - LOG.error(e.message, e) - res.send(JsonRpcResponse(id = jsonRpcRequest.id, error = e.message ?: "Error")) + if (!(e is ServiceException)) LOG.error(e.message, e) + res.send(JsonRpcResponse(id = jsonRpcRequest.id, error = e.message ?: "Error", + exceptionType = e.javaClass.canonicalName)) } } } else { diff --git a/kvision-modules/kvision-server-ktor/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt b/kvision-modules/kvision-server-ktor/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt index e4a48bc3..62ce4f0c 100644 --- a/kvision-modules/kvision-server-ktor/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt +++ b/kvision-modules/kvision-server-ktor/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt @@ -124,11 +124,12 @@ actual open class KVServiceManager<T : Any> actual constructor(val serviceClass: ) ) } catch (e: Exception) { - LOG.error(e.message, e) + if (!(e is ServiceException)) LOG.error(e.message, e) call.respond( JsonRpcResponse( id = jsonRpcRequest.id, - error = e.message ?: "Error" + error = e.message ?: "Error", + exceptionType = e.javaClass.canonicalName ) ) } @@ -163,11 +164,12 @@ actual open class KVServiceManager<T : Any> actual constructor(val serviceClass: ) ) } catch (e: Exception) { - LOG.error(e.message, e) + if (!(e is ServiceException)) LOG.error(e.message, e) call.respond( JsonRpcResponse( id = jsonRpcRequest.id, - error = e.message ?: "Error" + error = e.message ?: "Error", + exceptionType = e.javaClass.canonicalName ) ) } @@ -211,11 +213,12 @@ actual open class KVServiceManager<T : Any> actual constructor(val serviceClass: ) ) } catch (e: Exception) { - LOG.error(e.message, e) + if (!(e is ServiceException)) LOG.error(e.message, e) call.respond( JsonRpcResponse( id = jsonRpcRequest.id, - error = e.message ?: "Error" + error = e.message ?: "Error", + exceptionType = e.javaClass.canonicalName ) ) } @@ -261,11 +264,12 @@ actual open class KVServiceManager<T : Any> actual constructor(val serviceClass: ) ) } catch (e: Exception) { - LOG.error(e.message, e) + if (!(e is ServiceException)) LOG.error(e.message, e) call.respond( JsonRpcResponse( id = jsonRpcRequest.id, - error = e.message ?: "Error" + error = e.message ?: "Error", + exceptionType = e.javaClass.canonicalName ) ) } @@ -312,11 +316,12 @@ actual open class KVServiceManager<T : Any> actual constructor(val serviceClass: ) ) } catch (e: Exception) { - LOG.error(e.message, e) + if (!(e is ServiceException)) LOG.error(e.message, e) call.respond( JsonRpcResponse( id = jsonRpcRequest.id, - error = e.message ?: "Error" + error = e.message ?: "Error", + exceptionType = e.javaClass.canonicalName ) ) } @@ -365,11 +370,12 @@ actual open class KVServiceManager<T : Any> actual constructor(val serviceClass: ) ) } catch (e: Exception) { - LOG.error(e.message, e) + if (!(e is ServiceException)) LOG.error(e.message, e) call.respond( JsonRpcResponse( id = jsonRpcRequest.id, - error = e.message ?: "Error" + error = e.message ?: "Error", + exceptionType = e.javaClass.canonicalName ) ) } @@ -464,11 +470,12 @@ actual open class KVServiceManager<T : Any> actual constructor(val serviceClass: ) ) } catch (e: Exception) { - LOG.error(e.message, e) + if (!(e is ServiceException)) LOG.error(e.message, e) call.respond( JsonRpcResponse( id = jsonRpcRequest.id, - error = e.message ?: "Error" + error = e.message ?: "Error", + exceptionType = e.javaClass.canonicalName ) ) } @@ -511,11 +518,12 @@ actual open class KVServiceManager<T : Any> actual constructor(val serviceClass: ) ) } catch (e: Exception) { - LOG.error(e.message, e) + if (!(e is ServiceException)) LOG.error(e.message, e) call.respond( JsonRpcResponse( id = jsonRpcRequest.id, - error = e.message ?: "Error" + error = e.message ?: "Error", + exceptionType = e.javaClass.canonicalName ) ) } diff --git a/kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt b/kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt index fb6b6210..17451461 100644 --- a/kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt +++ b/kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt @@ -146,12 +146,13 @@ actual open class KVServiceManager<T : Any> actual constructor(val serviceClass: ) ) } catch (e: Exception) { - LOG.error(e.message, e) + if (!(e is ServiceException)) LOG.error(e.message, e) ServerResponse.ok().json().bodyValueAndAwait( mapper.writeValueAsString( JsonRpcResponse( id = jsonRpcRequest.id, - error = e.message ?: "Error" + error = e.message ?: "Error", + exceptionType = e.javaClass.canonicalName ) ) ) @@ -190,12 +191,13 @@ actual open class KVServiceManager<T : Any> actual constructor(val serviceClass: ) ) } catch (e: Exception) { - LOG.error(e.message, e) + if (!(e is ServiceException)) LOG.error(e.message, e) ServerResponse.ok().json().bodyValueAndAwait( mapper.writeValueAsString( JsonRpcResponse( id = jsonRpcRequest.id, - error = e.message ?: "Error" + error = e.message ?: "Error", + exceptionType = e.javaClass.canonicalName ) ) ) @@ -245,12 +247,13 @@ actual open class KVServiceManager<T : Any> actual constructor(val serviceClass: ) ) } catch (e: Exception) { - LOG.error(e.message, e) + if (!(e is ServiceException)) LOG.error(e.message, e) ServerResponse.ok().json().bodyValueAndAwait( mapper.writeValueAsString( JsonRpcResponse( id = jsonRpcRequest.id, - error = e.message ?: "Error" + error = e.message ?: "Error", + exceptionType = e.javaClass.canonicalName ) ) ) @@ -302,12 +305,13 @@ actual open class KVServiceManager<T : Any> actual constructor(val serviceClass: ) ) } catch (e: Exception) { - LOG.error(e.message, e) + if (!(e is ServiceException)) LOG.error(e.message, e) ServerResponse.ok().json().bodyValueAndAwait( mapper.writeValueAsString( JsonRpcResponse( id = jsonRpcRequest.id, - error = e.message ?: "Error" + error = e.message ?: "Error", + exceptionType = e.javaClass.canonicalName ) ) ) @@ -360,12 +364,13 @@ actual open class KVServiceManager<T : Any> actual constructor(val serviceClass: ) ) } catch (e: Exception) { - LOG.error(e.message, e) + if (!(e is ServiceException)) LOG.error(e.message, e) ServerResponse.ok().json().bodyValueAndAwait( mapper.writeValueAsString( JsonRpcResponse( id = jsonRpcRequest.id, - error = e.message ?: "Error" + error = e.message ?: "Error", + exceptionType = e.javaClass.canonicalName ) ) ) @@ -420,12 +425,13 @@ actual open class KVServiceManager<T : Any> actual constructor(val serviceClass: ) ) } catch (e: Exception) { - LOG.error(e.message, e) + if (!(e is ServiceException)) LOG.error(e.message, e) ServerResponse.ok().json().bodyValueAndAwait( mapper.writeValueAsString( JsonRpcResponse( id = jsonRpcRequest.id, - error = e.message ?: "Error" + error = e.message ?: "Error", + exceptionType = e.javaClass.canonicalName ) ) ) @@ -530,12 +536,13 @@ actual open class KVServiceManager<T : Any> actual constructor(val serviceClass: ) ) } catch (e: Exception) { - LOG.error(e.message, e) + if (!(e is ServiceException)) LOG.error(e.message, e) ServerResponse.ok().json().bodyValueAndAwait( mapper.writeValueAsString( JsonRpcResponse( id = jsonRpcRequest.id, - error = e.message ?: "Error" + error = e.message ?: "Error", + exceptionType = e.javaClass.canonicalName ) ) ) @@ -584,12 +591,13 @@ actual open class KVServiceManager<T : Any> actual constructor(val serviceClass: ) ) } catch (e: Exception) { - LOG.error(e.message, e) + if (!(e is ServiceException)) LOG.error(e.message, e) ServerResponse.ok().json().bodyValueAndAwait( mapper.writeValueAsString( JsonRpcResponse( id = jsonRpcRequest.id, - error = e.message ?: "Error" + error = e.message ?: "Error", + exceptionType = e.javaClass.canonicalName ) ) ) |