-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7657791
commit 7c94d30
Showing
6 changed files
with
130 additions
and
50 deletions.
There are no files selected for viewing
This file contains 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 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 akumu | ||
|
||
import ( | ||
"net/http" | ||
"sync" | ||
) | ||
|
||
type ContextKey struct{} | ||
|
||
type OnErrorNext func(ServerError) | ||
type OnErrorCallback func(ServerError, OnErrorNext) | ||
|
||
// Contextual is a concurrent-safe implementation | ||
// to append akumu-specific context to the given request. | ||
type Contextual struct { | ||
mutex sync.Mutex | ||
onError []OnErrorCallback | ||
} | ||
|
||
func NewContext() *Contextual { | ||
return &Contextual{} | ||
} | ||
|
||
func Context(request *http.Request) (*Contextual, bool) { | ||
ctx, ok := request.Context().Value(ContextKey{}).(*Contextual) | ||
|
||
return ctx, ok | ||
} | ||
|
||
func (ctx *Contextual) OnError(callback OnErrorCallback) { | ||
ctx.mutex.Lock() | ||
defer ctx.mutex.Unlock() | ||
|
||
ctx.onError = append(ctx.onError, callback) | ||
} | ||
|
||
func (ctx *Contextual) handleError(err ServerError) { | ||
ctx.mutex.Lock() | ||
defer ctx.mutex.Unlock() | ||
|
||
var index int | ||
var next func(ServerError) | ||
|
||
next = func(err ServerError) { | ||
if index < len(ctx.onError) { | ||
callback := ctx.onError[index] | ||
index++ | ||
callback(err, next) | ||
} | ||
} | ||
|
||
next(err) | ||
} |
This file contains 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,28 @@ | ||
package akumu | ||
|
||
import "fmt" | ||
|
||
type ServerErrorKind string | ||
|
||
type ServerError struct { | ||
Code int | ||
URL string | ||
Text string | ||
Kind ServerErrorKind | ||
Body string | ||
} | ||
|
||
var ( | ||
ServerErrorWriter ServerErrorKind = "writer" | ||
ServerErrorBody ServerErrorKind = "body" | ||
ServerErrorStream ServerErrorKind = "stream" | ||
ServerErrorDefault ServerErrorKind = "default" | ||
) | ||
|
||
func (err ServerError) Error() string { | ||
return fmt.Sprintf( | ||
"%d: %s", | ||
err.Code, | ||
err.Text, | ||
) | ||
} |
This file contains 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 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