Skip to content

Commit

Permalink
minor fixes, set response code to 200 before calling api enforcer
Browse files Browse the repository at this point in the history
  • Loading branch information
muir committed Oct 19, 2021
1 parent fd1d826 commit fcacf3f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
13 changes: 8 additions & 5 deletions nject/example_db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,34 @@ import (
"github.com/muir/nject/nject"
)

// InjectDB injects both a *sql.DB and *sql.Tx if they're needed.
// InjectDB injects both an *sql.DB and an *sql.Tx if they're needed.
// Errors from opening and closing the database can be returned
// so a consumer of downstream errors is necessary.
// A context.Context is used in the creation of the transaction
// inject that earlier in the chain. txOptions can be nil.
// inject that earlier in the chain. txOptions can be nil. If a
// transaction is injected, it will be automatically committed if the
// returned error from downstream is nil. It will be rolled back if
// the returned error is not nil.
func InjectDB(driver, uri string, txOptions *sql.TxOptions) *nject.Collection {
return nject.Sequence("database-sequence",
driverType(driver),
uriType(uri),
txOptions,

// We tag the db injector as MustConsume so that we don't inject
// the database unless the is a consumer for it. When a wrapper
// the database unless there is a consumer for it. When a wrapper
// returns error, it should usually consume error too and pass
// that error along, otherwise it can mask a downstream error.
nject.MustConsume(nject.Provide("db", injectDB)),

// We tag the tx injector as MustConsume so that we don't inject
// the transaction unless the is a consumer for it. When a wrapper
// the transaction unless there is a consumer for it. When a wrapper
// returns error, it should usually consume error too and pass
// that error along, otherwise it can mask a downstream error.
nject.MustConsume(nject.Provide("tx", injectTx)),

// Since injectTx or injectDB consumes an error, this provider
// will supply that error if there is no other downstream supplier
// will supply that error if there is no other downstream supplier.
nject.Shun(nject.Provide("fallback error", fallbackErrorSource)),
)
}
Expand Down
3 changes: 3 additions & 0 deletions nvelope/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ one place where API enforcement can be done. The type `nvelope.Body` is provide
This is a user-provided optional step that can be used to double-check
that what is being sent matches the API defintion.

The [nvalid](https://github.com/muir/nvalid) package provides a function
to generate a response validator from Swagger.

### Decode the request body

The request body needs to be unpacked with an unmarshaller of some kind.
Expand Down
3 changes: 3 additions & 0 deletions nvelope/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,9 @@ func MakeResponseEncoder(
}
}

if code == 0 {
code = 200
}
err = encoder.apiEnforcer(code, enc, w.Header(), r)
if err != nil {
handleError(true)
Expand Down

0 comments on commit fcacf3f

Please # to comment.