Client error Interceptors #2482
Replies: 5 comments 3 replies
|
This looks like a good enhancement we can add to the SDKs. I like the idea. |
|
The feature would be nice, but not urgent. For example, I have written a simple wrapper that takes care of this. Of course you always have to pass the function call to this wrapper, but this is not very complex and relatively flexible, if you want to quickly add edge cases, for example. In addition, you make each call to AppWrite in the best case anyway only at one point in the code. |
|
I like the idea. I think we could expand on it to include generalised interceptors, where they could be applied to the request, response or error. Then developers could add an error interceptor as above, but could also add other custom interceptors like logging, analytics, retry handlers etc. It would be helpful for debugging issues too. |
|
@abnegate @lohanidamodar do you have any code examples of how you would imagine this functionally in the client object? |
|
@eldadfux Something like so in my mind: SDK Codeinternal interface Interceptor<T> {
fun intercept(obj: T): T
}
interface RequestInterceptor: Interceptor<Request> {
fun intercept(request: Request) : Request
}
interface ErrorInterceptor: Interceptor<Throwable> {
fun intercept(throwable: Throwable) : Throwable
}Client Additionsval interceptors: MutableList<Interceptor<*>>
fun addInterceptor(interceptor: Interceptor) {
interceptors.add(interceptor)
}// ...
// On error thrown
catch(ex: Exception) {
interceptors
.find { it is ErrorInterceptor }
.forEach { it.intercept(ex) }
}
// ...Developer Codeclass MyErrorInterceptor : ErrorInterceptor {
fun intercept(throwable: Throwable): Throwable {
print("Encountered error: ${throwable.message}")
return throwable
}
}client.addInterceptor(MyErrorInterceptor()); |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
I have been using Appwrite with Flutter for a couple of days, and I noticed a glaring issue that should be rather easy to implement.
It is the lack of flexibility when handling errors. For example, if you receive a 401, it would be nice to have an error interceptor that handles that case globally and signs you out of the app.
Right now, the solution for that would be calling account.get() to try and retrieve a session, but I find that too cumbersome and a suboptimal replacement for a simple error interceptor. What are your thoughts on this ?
My proposal would be to have something like this :
class MyInterceptor extends ErrorInterceptor{
@override
FutureOr onError(AppwriteException exception) {
if (exception.code==401) {
// Sign user out of the app
}
return exception;
}
}
All reactions