-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new x/errors package to handle HTTP wire errors
- Loading branch information
Showing
9 changed files
with
646 additions
and
2 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
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
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,110 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/kataras/iris/v12" | ||
// IMPORTANT, import this sub-package. | ||
// Note tht it does NOT break compatibility with the | ||
// standard "errors" package as the New, | ||
// Is, As, Unwrap functions are aliases to the standard package. | ||
"github.com/kataras/iris/v12/x/errors" | ||
) | ||
|
||
// Optionally, register custom error codes. | ||
// | ||
// The default list of error code names: | ||
// errors.Cancelled | ||
// errors.Unknown | ||
// errors.InvalidArgument | ||
// errors.DeadlineExceeded | ||
// errors.NotFound | ||
// errors.AlreadyExists | ||
// errors.PermissionDenied | ||
// errors.Unauthenticated | ||
// errors.ResourceExhausted | ||
// errors.FailedPrecondition | ||
// errors.Aborted | ||
// errors.OutOfRange | ||
// errors.Unimplemented | ||
// errors.Internal | ||
// errors.Unavailable | ||
// errors.DataLoss | ||
var ( | ||
Custom = errors.E("CUSTOM_CANONICAL_ERROR_NAME", iris.StatusBadRequest) | ||
) | ||
|
||
func main() { | ||
app := iris.New() | ||
|
||
// Custom error code name. | ||
app.Get("/custom", fireCustomErrorCodeName) | ||
|
||
// Send a simple 400 request with message and an error | ||
// or with more details and data. | ||
app.Post("/invalid_argument", fireInvalidArgument) | ||
|
||
// Compatibility with the iris.Problem type (and any other custom type). | ||
app.Get("/problem", fireErrorWithProblem) | ||
|
||
app.Listen(":8080") | ||
} | ||
|
||
func fireCustomErrorCodeName(ctx iris.Context) { | ||
Custom.Details(ctx, "message", "details with arguments: %s", "an argument") | ||
} | ||
|
||
func fireInvalidArgument(ctx iris.Context) { | ||
var req = struct { | ||
Username string `json:"username"` | ||
}{} | ||
if err := ctx.ReadJSON(&req); err != nil { | ||
errors.InvalidArgument.Err(ctx, err) | ||
return | ||
} | ||
|
||
ctx.WriteString(req.Username) | ||
|
||
// Other examples: errors.InvalidArgument/NotFound/Internal and e.t.c. | ||
// .Message(ctx, "message %s", "optional argument") | ||
// .Details(ctx, "message", "details %s", "optional details argument") | ||
// .Data(ctx, "message", anyTypeOfValue) | ||
// .DataWithDetails(ctx, "unable to read the body", "malformed json", iris.Map{"custom": "data of any type"}) | ||
// .Log(ctx, "message %s", "optional argument") | ||
// .LogErr(ctx, err) | ||
} | ||
|
||
func fireErrorWithProblem(ctx iris.Context) { | ||
myCondition := true | ||
if myCondition { | ||
problem := iris.NewProblem(). | ||
// The type URI, if relative it automatically convert to absolute. | ||
Type("/product-error"). | ||
// The title, if empty then it gets it from the status code. | ||
Title("Product validation problem"). | ||
// Any optional details. | ||
Detail("details about the product error"). | ||
// The status error code of the problem, can be optional here. | ||
// Status(iris.StatusBadRequest). | ||
// Any custom key-value pair. | ||
Key("product_name", "the product name") | ||
|
||
errors.InvalidArgument.Data(ctx, "unable to process the request", problem) | ||
return | ||
|
||
/* Prints to the client: | ||
{ | ||
"http_error_code": { | ||
"canonical_name": "INVALID_ARGUMENT", | ||
"status": 400 | ||
}, | ||
"message": "unable to process the request", | ||
"data": { | ||
"detail": "details about the product error", | ||
"product_name": "the product name", | ||
"title": "Product validation problem", | ||
"type": "/product-error" | ||
} | ||
} | ||
*/ | ||
} | ||
|
||
} |
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,25 @@ | ||
package errors | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
) | ||
|
||
var ( | ||
// Is is an alias of the standard errors.Is function. | ||
Is = errors.Is | ||
// As is an alias of the standard errors.As function. | ||
As = errors.As | ||
// New is an alias of the standard errors.New function. | ||
New = errors.New | ||
// Unwrap is an alias of the standard errors.Unwrap function. | ||
Unwrap = errors.Unwrap | ||
) | ||
|
||
func sprintf(format string, args ...interface{}) string { | ||
if len(args) > 0 { | ||
return fmt.Sprintf(format, args...) | ||
} | ||
|
||
return format | ||
} |
Oops, something went wrong.