Comments (7)
We could maybe change the conversion of errors to rest.Error
to rely on errors.As
(new in Go 1.13).
Then you can implement your own error types as needed, and implement the following method to asign itself to a rest.Error
:
func (err MyError) As(target interface{}) bool {
v, ok := target.(*http.Error)
if !ok { return false }
// set v. values
return true
}
We could for conveniences provide something like this in the rest
package:
type codeError struct{
code int
err error
}
func StatusError(code int, err error) error {
return codeError{code: code, err: err}
}
func (err codeError) Unwrap() error {
return err.err
}
func (err codeError) Error() string {
if err.err == nil {
return http.StatusText(err.code)
}
return err.Error()
}
func (err codeError) As(target interface{}) bool {
v, ok := target.(*http.Error)
if !ok { return false }
v.Code = err.Code()
v.Message = err.Error()
return true
If we do the conversion to rest.Error
in the error formatter, then your "original" error could be logged to sentry first.
from rest-layer.
There are other ways to do this, this is just one method.
from rest-layer.
Thank you @smyrman for the detailed response. Let me think about it a little.
from rest-layer.
Likewise we can match against errors from resource
. (e.g. resource.ErrNotFound
) using errors.Is
, so that we will match even when they are wrapped by other errors, and thus be able to get a rest.Error
instance with more context in the error message.
To wrap an error in the most plain way possible, you can use e.g. fmt.Errorf("prefix: %w", resource.ErrNotFound)
or fmt.Errorf("%w: suffix", resource.ErrNotFound)
in hooks etc.
Should change relevant places where errors are returned to return non-pointer errors.
from rest-layer.
Actually I am using another error package for hooks: github.com/rotisserie/eris
, which can record file:location
for each error invocation/wrap, thus getting exhaustive traces for debugging.
Maybe we can still migrate rest-later
to Go 1.13 errors
, on order to propagate errors from hooks.
from rest-layer.
Hi @smyrman , sorry for the big delay. Regarding this issue, can you take a look at this patch:
Dragomir-Ivanov@2e9eb16
It seems to work, without introducing new types. We can change the NewError
function name if you like.
from rest-layer.
Which issue does this patch solve? It seams to return exactly the same as before, excpet that the NewError function returns two values, where one is now a generic error
type?
from rest-layer.
Related Issues (20)
- Remove old sub-resource syntax HOT 4
- go module: cannot find module providing package testing/mem HOT 3
- Explicit $eq in filter passes whole predicate to Validate(Query) HOT 3
- OnInsert/OnUpdate hooks can modify the new resource item, Etag not recalculated HOT 7
- `Prefer: return=minimal` can hide resource item on server modification HOT 12
- Consistent empty / null fields HOT 4
- Remove resource hooks in favor of resource middleware HOT 12
- Newest `zerolog` versions cause a panic HOT 4
- Bogus error line, when returning empty response on 204
- Etag should not depend on external state HOT 4
- Assist AutoIncrement FieldValidator with SQL backend HOT 8
- How use schema and resource package as standalone HOT 1
- Bulk PATCH support HOT 5
- Feature Request: Cursor-based Pagination HOT 3
- pq: operator does not exist: uuid ~~ unknown HOT 7
- Filter on reference fields HOT 1
- Support for $not filter operation HOT 4
- Decouple ElemMatch from schema.Array struct
- openapi 3.1.0 support HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rest-layer.