tryPatch

suspend fun <T> tryPatch(resource: String, body: RequestBody? = null, headers: Map<String, Any>? = FetchDefaults.Headers, redirect: RequestRedirect? = FetchDefaults.Redirect, abortController: AbortController? = null, transform: suspend Response.() -> T): T?(source)

Like patch, but returns null instead of throwing if the request fails.

This method also provides a transform step which you can use to convert the response into a different type. You are generally encouraged to call tryPatch(...) { convert() } over tryPatch(...)?.convert() as the former will ensure that exception handling is covered in that case.

Additionally, if logOnError is set to true, any failure will be logged to the console (including the logic in the transform block).

If you do not care about converting the result, use the tryPatch version that returns Response? instead.


suspend fun tryPatch(resource: String, body: RequestBody? = null, headers: Map<String, Any>? = FetchDefaults.Headers, redirect: RequestRedirect? = FetchDefaults.Redirect, abortController: AbortController? = null): Response?(source)

Like patch, but returns null instead of throwing if the request fails.

Additionally, if logOnError is set to true, any failure will be logged to the console.

If you plan to do additional operations on the response and would also like to have logging / exception protection for them, consider using the other tryPatch call which lets you pass in a transform callback. You are generally encouraged to call tryPatch(...) { convert() } over tryPatch(...)?.convert().