This tutorial continues Developing a RESTful API with Go and Gin featured in Go documentation. Please check it first.
TL;DR We're going to replace...
For further actions, you may consider blocking this person and/or reporting abuse
Is it possible to return a JSON object for error? For example, on the client side, we need to differentiate various kinds of 400(bad request errors), but we don't want to parse the returned string. So we created a simple struct type
which, in the case of Gin, would be returned by using
c.JSON(HTTP.StatusBadRequest, errorResponse)
. I cannot wrap my head completely into using usecase interactions, so that's the only thing that stops me to migrate away from using Gin (which I don't like for their approach to validation)Thanks
PS: Great tutorial
This can be done on a handler processor level, with
MakeErrResp
configuration.Please check an example:
github.com/swaggest/rest/blob/v0.2...
Probably it would be good to have a special interface that could be implemented on a custom error instance to simplify the configuration. 🤔
Thank you for your reply. I saw this example and even (before seeing it) came up with a solution to add a handler (for error logic response generation) as an extra argument to
service.Post()
. But after doing all that, I started thinking of this problem in terms of supporting multiple kinds of400
(and other) errors such that my implemented "use cases" would embed all needed details into the returnederror
.In this case, the responsibility of the service-wise wrapper would be to identify if the error is of a special kind, and if so, then return the aforementioned struct with 2 fields directly taken from the prepared error object
I think this behavior partially matches the implemented one, but I need to update it to exactly meet the expectation, and here is my main issue:
I find it challenging that the need to build a custom logic requires extensively studying the internals of the library implementation. Not only the examples but the sense of how these examples work. The
http.Handler
andnethttp.Handler
are often confused after a brief look through the sources and require a few extra moments to adjust the thinking.Lastly, the tight incorporation of the
chi
router into the implementation in the sense that many internal structs havechi
structs embedded, leaves no option but to get rid of the currently used router (gin
in my case) and adoptchi
instead. I have no objection to usingchi
, but the lack of flexibility in the early phase of the library adoption doesn't make it an easy walk.Interesting, now I'm a bit puzzled about your current setup. Could you make a small example app that matches your current approach (with a "misbehaving" error structure)?
The library is built around
chi
, because router is an essential part of a web application. But I'd be happy to make it easier to use with other cases. As far as I understood, you usegin
with response encoder fromswaggest/rest
. There is an example of integration withgin
, but it only focuses on documentation, leaving request decoding and response writing to gin's standard facilities.We first define a bunch of constants like this
to address various scenarios of 404 (and other kinds of) errors. Each constant is of
type ErrorCode int
(used to generate separate documentation for all errors as illustrated in app.poc.quible.tech/api/v1/docs/er... to be used by the client). The big goal is to introduce "numbers" instead of "strings" for the client to identify the error reason.We then define a map to associate numerical codes and short descriptions (as shown in the page above) and use that map when a certain
gin
handler needs to respond with a specific error. For example,SendError(c, http.StatusBadRequest, Err400_InvalidRequestBody)
I am looking for a plug&play replacement of
SendError
that can be fed with const likeErr400_InvalidRequestBody
to produce an error returned from the usecase, such that, that error would be handled elsewhere to result in responding with JSON objectsto clients, given that
code
andmessage
are those, linked in the error map.Great work!
One of a kind.
I don't want to abuse, you helped me the past week. I need to set a cookie in a NewIterator implementation, but I don't get how to use the responseWriter to do
http.setCookie(w, cookie)
Could you please tell me some tips.
Ok.. I've done this solution, but I don't get proper documentation on swagger using .mount :(
By the way, as much I use your your repo I could say it is very very complete...
Very nice work!!.. I would like to be able to return (when paginating) an array in the response body and the total count of the query in a header response X-Total-Count.
But If I set a struct for the response like for instance
// Declare output port type.
type helloOutput struct {
TotalCount int64 header:"X-Total-Count" json:"-"
Data []Vehicles json:"vehicles"
}
I need the response to be
[{
vehicle:1
},{
vehicle:2}
]
and NOT
{
data: [{
vehicle:1
},{
vehicle:2}
]
}
Is there any suggestion to make it possible with the usecase callback function?
Replied at github.com/swaggest/rest/issues/95....
Well done! Nice and clean! :)
I would recommend to support openapi.yaml as well.
Thank you!
Serving
openapi.yaml
is relatively simple, you just need to marshal spec as YAML and attach http handler to service.Very Nice, I have done this solution and Works. Thank you.