Skip to content
This repository has been archived by the owner on Dec 12, 2024. It is now read-only.

http-server: submitOrder and submitClose #119

Merged
merged 17 commits into from
Jan 10, 2024
Merged

Conversation

phoebe-lew
Copy link
Contributor

@phoebe-lew phoebe-lew commented Dec 19, 2023

closes #117
closes #26
closes #27

Copy link

codecov bot commented Dec 20, 2023

Codecov Report

Merging #119 (683c1ce) into main (d549fba) will increase coverage by 1.03%.
The diff coverage is 67.76%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #119      +/-   ##
==========================================
+ Coverage   77.51%   78.54%   +1.03%     
==========================================
  Files          26       30       +4     
  Lines         556      662     +106     
  Branches       47       63      +16     
==========================================
+ Hits          431      520      +89     
- Misses        100      110      +10     
- Partials       25       32       +7     
Components Coverage Δ
protocol 86.09% <58.33%> (-0.65%) ⬇️
httpclient 82.73% <ø> (ø)

} catch (e: CallbackError) {
call.respond(e.statusCode, ErrorResponse(e.details))
return
} catch (e: Exception) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i am seeing this when writing the GET implementations as well - currently there doesn't seem to be a way to have the callback fail so we can check that the exception's thrown correctly and correct http codes returned in the call.respond(..

Copy link
Contributor

@andresuribe87 andresuribe87 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

General comments that I consider important, but nothing that I consider blocking.

Awesome stuff!

* @param callback The optional callback function to be executed after a successful close submission.
*/
@Suppress("TooGenericExceptionCaught", "MaxLineLength")
suspend fun submitClose(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Throughout the web5-kt codebase we've chosen to have exceptions be types, with the name of the exception signaling what the error was. An example of this is https://github.com/TBD54566975/web5-kt/blob/48ce6764163b32714be7d8282bfad0678f49debb/credentials/src/main/kotlin/web5/sdk/credentials/ExceptionDeclarations.kt#L10

The rationale of such approach was discussed in decentralized-identity/web5-kt#144

Is this something that makes sense to adopt in tbdex-kt?

If so, I'd suggest implementing as shown below, using a custom plugin in ktor:

val ExceptionsPlugin = createApplicationPlugin(name = "ExceptionsPlugin") {
  println("ExceptionsPlugin is installed!")
  on(CallFailed) { call, cause ->
    when(cause) {
      is MyCustomException -> {
        val errorDetail = ErrorDetail(detail = cause.message.orEmpty())
        val errorResponse = ErrorResponse(listOf(errorDetail))
        call.respond(HttpStatusCode.BadRequest, errorResponse)
      }
      else -> {
        println("Unknown error: ${cause.message}")
        call.respond(HttpStatusCode.BadRequest, ErrorResponse(listOf(ErrorDetail(detail = cause.message.orEmpty()))))
      }
    }
  }
}

The plugin can be installed in the application by doing the following within the configure method.

app.install(ExceptionsPlugin)

Personally, I'd advocate for this approach, as it decouples the translation from Exceptions to ErrorResponse types into a single place (the plugin). This makes it very easy to reuse Exceptions. Additionally, it ensures that the server always responds with ErrorResponse, even in the event of what unexpected exceptions being thrown (though perhaps that's caught elsewhere in the ktor framework!).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh awesome, thanks! Yeah I think we are aligning on something similar in tbdex-js too, so it would make sense to adopt here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

try {
callback.invoke(call, MessageKind.close, null)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Callbacks are another case for which I would suggest using the plugin approach. We used a similar approach when defining Webhooks in ssi-service. A middleware-esque approach. The advantage is code decoupling and separation of concerns, which allows for code to be more easily modified in the future.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the right reference for how plugins work? https://ktor.io/docs/plugins.html

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. I was looking at https://ktor.io/docs/custom-plugins.html more specifically

httpserver/src/test/kotlin/TestData.kt Show resolved Hide resolved
phoebe-lew and others added 7 commits December 22, 2023 09:47
* adding getofferings and getexchanges req handler

* adding getofferings and getexchanges req handler

* adding tests

* Fix test

* adding docs

* Fix imports

---------

Co-authored-by: Phoebe Lew <[email protected]>
Copy link
Contributor

@jiyoonie9 jiyoonie9 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@phoebe-lew phoebe-lew merged commit 824df4b into main Jan 10, 2024
2 of 5 checks passed
@phoebe-lew phoebe-lew deleted the plew.http-server-submits branch January 10, 2024 20:44
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement valid-next functionality Implement submit order handler Implement submit close handler
4 participants