-
Notifications
You must be signed in to change notification settings - Fork 1
impl: improved logging and error collection for the http client #165
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
4f1c279
impl: relax json syntax rules for deserialization
fioan89 50da220
Merge branch 'main' into impl-lenient-http-client
fioan89 7d4a8ad
impl: add error logging for malformed JSON responses in Coder REST AP…
fioan89 8fca449
impl: configuration for controlling the rest api client log level
fioan89 f8606a0
impl: configure the rest api client log level from the Settings page
fioan89 4cab216
impl: log the rest api client request and response
fioan89 8a3a698
chore: remove support for lenient moshi marshaller
fioan89 2decf9b
chore: refactor interceptor around the kotlin buildString DSL
fioan89 3899d77
doc: how to enable http logging
fioan89 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
src/main/kotlin/com/coder/toolbox/sdk/convertors/LoggingConverterFactory.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.coder.toolbox.sdk.convertors | ||
|
||
import com.coder.toolbox.CoderToolboxContext | ||
import okhttp3.RequestBody | ||
import okhttp3.ResponseBody | ||
import retrofit2.Converter | ||
import retrofit2.Retrofit | ||
import java.lang.reflect.Type | ||
|
||
class LoggingConverterFactory private constructor( | ||
private val context: CoderToolboxContext, | ||
private val delegate: Converter.Factory, | ||
) : Converter.Factory() { | ||
|
||
override fun responseBodyConverter( | ||
type: Type, | ||
annotations: Array<Annotation>, | ||
retrofit: Retrofit | ||
): Converter<ResponseBody, *>? { | ||
// Get the delegate converter | ||
val delegateConverter = delegate.responseBodyConverter(type, annotations, retrofit) | ||
?: return null | ||
|
||
@Suppress("UNCHECKED_CAST") | ||
return LoggingMoshiConverter(context, delegateConverter as Converter<ResponseBody, Any?>) | ||
} | ||
|
||
override fun requestBodyConverter( | ||
type: Type, | ||
parameterAnnotations: Array<Annotation>, | ||
methodAnnotations: Array<Annotation>, | ||
retrofit: Retrofit | ||
): Converter<*, RequestBody>? { | ||
return delegate.requestBodyConverter(type, parameterAnnotations, methodAnnotations, retrofit) | ||
} | ||
|
||
override fun stringConverter( | ||
type: Type, | ||
annotations: Array<Annotation>, | ||
retrofit: Retrofit | ||
): Converter<*, String>? { | ||
return delegate.stringConverter(type, annotations, retrofit) | ||
} | ||
|
||
companion object { | ||
fun wrap( | ||
context: CoderToolboxContext, | ||
delegate: Converter.Factory, | ||
): LoggingConverterFactory { | ||
return LoggingConverterFactory(context, delegate) | ||
} | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/main/kotlin/com/coder/toolbox/sdk/convertors/LoggingMoshiConverter.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.coder.toolbox.sdk.convertors | ||
|
||
import com.coder.toolbox.CoderToolboxContext | ||
import okhttp3.ResponseBody | ||
import okhttp3.ResponseBody.Companion.toResponseBody | ||
import retrofit2.Converter | ||
|
||
class LoggingMoshiConverter( | ||
private val context: CoderToolboxContext, | ||
private val delegate: Converter<ResponseBody, Any?> | ||
) : Converter<ResponseBody, Any> { | ||
|
||
override fun convert(value: ResponseBody): Any? { | ||
val bodyString = value.string() | ||
|
||
return try { | ||
// Parse with Moshi | ||
delegate.convert(bodyString.toResponseBody(value.contentType())) | ||
} catch (e: Exception) { | ||
// Log the raw content that failed to parse | ||
context.logger.error( | ||
""" | ||
|Moshi parsing failed: | ||
|Content-Type: ${value.contentType()} | ||
|Content: $bodyString | ||
|Error: ${e.message} | ||
""".trimMargin() | ||
) | ||
|
||
// Re-throw so the onFailure callback still gets called | ||
throw e | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.