New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Nicer way to optionally get a ContextPlugin extension #2102
Comments
Hi @juggernaut0, could you tell us a bit more about your use case? How does the program recover if the plugin is not there? |
I have some utility functions and plugins that are used across several projects, but not every project needs every plugin. The code just assumes if the plugin is not there, it's not relevant to that app, so it skips a block and continues on. There's plenty of workarounds like the try-catch, using nullable Basically, the idea is augmenting behavior based on a plugin, but not requiring it to be installed. FWIW I'm translating code from Ktor, which has a |
So something like logging, tracing, analytics?
I supposed we could have I'm not a fan of how |
Wouldn't it be easier to just cover this with app data? // Javalin's key
data class Key<T>(val id: String)
// Define a key
val OptionalMetrics = Key<Metrics?>("optional-metrics")
// Register data
cfg.pvt.appAttributes[OptionalMetrics] // null or not
// Handler
ctx.appData(OptionalMetrics)?.example("abc") I'm not sure why there's no public register function for app data, so @tipsy may need to clarify this area. |
From https://javalin.io/documentation#app-data: val app = Javalin.create { config ->
config.appData(myKey, myValue)
} |
Okay, my bad, I was looking at |
Describe the feature
A method on context similar to
with(MyPlugin::class)
but does not throw if the given plugin is not registered. It would simply return null instead.Additional context
I am developing a utility library that can optionally be augmented by a plugin, and I couldn't find a way into the private pluginManager object, so right now I need to surround the
ctx.with
call with a try-catch to catch the PluginNotRegisteredException which is a little gross.I haven't measured but I assume there is a performance overhead to throwing and catching an exception.
The text was updated successfully, but these errors were encountered: