Giter Site home page Giter Site logo

fn's People

Contributors

deardrops avatar lonng avatar lucklove avatar maplefu avatar nexustar avatar tisonkun avatar zeromake avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

fn's Issues

Support for more custom request parsers

Here is my implementation

package main

import (
	"context"
	"net/http"
	"net/url"

	"github.com/pingcap/fn"
)
func main() {
	// Use reflection out(0) resp injection new request parser
	fn.SetRequestPlugin(func(ctx context.Context, r *http.Request) (url.Values, error) {
		return r.URL.Query(), nil
	})
	server := &http.Server{
		Addr:    ":8080",
		Handler: fn.Wrap(func (query url.Values) (string, error) {
			return query.Get("params"), nil
		}),
	}
	server.ListenAndServe()
}

unhandled error unpacking

demo:

package main

import (
	"context"
	"net/http"

	"github.com/gorilla/mux"
	"github.com/pingcap/fn"
	"github.com/pkg/errors"
)

var (
	// ErrTest define an error
	ErrTest = errors.New("test")
	errs    = map[error]int{
		ErrTest: 1001,
	}
)

// Unknown default error code
const Unknown = 1000

type statusCodeError struct {
	err        error
	statusCode int
}

func (s *statusCodeError) StatusCode() int {
	return s.statusCode
}

func (s *statusCodeError) Error() string {
	return s.err.Error()
}

func (s *statusCodeError) Unwrap() error {
	return s.err
}

// ErrorWithStatusCode Rewrite
func ErrorWithStatusCode(err error, statusCode int) error {
	return &statusCodeError{err, statusCode}
}

// Error1 fn.ErrorWithStatusCode wrapped error cannot be unpacked to the original error
func Error1() (string, error) {
	return "", fn.ErrorWithStatusCode(errors.WithStack(ErrTest), 500)
}

// Error2 ErrorWithStatusCode rewrite and implement the `StatusCode`, `Unwrap` interface to support the original error
func Error2() (string, error) {
	return "", ErrorWithStatusCode(errors.WithStack(ErrTest), 500)
}

// Error3 The ErrorWithStatusCode error can no longer be set with a status code if it is wrapped.
func Error3() (string, error) {
	return "", errors.WithStack(ErrorWithStatusCode(ErrTest, 500))
}

// Code 通过 error 获取 code 定义
func Code(err error) int {
	code, ok := errs[err]
	if !ok {
		for err != nil {
			err = errors.Unwrap(err)
			code, ok = errs[err]
			if ok {
				return code
			}
		}
		code = Unknown
	}
	return code
}

func main() {
	// generate a response with a status code via error
	fn.SetErrorEncoder(func(ctx context.Context, err error) interface{} {
		code := Code(err)
		return map[string]interface{}{
			"code":    code,
			"message": err.Error(),
		}
	})
	router := mux.NewRouter()
	router.Handle("/error1", fn.Wrap(Error1))
	router.Handle("/error2", fn.Wrap(Error2))
	router.Handle("/error3", fn.Wrap(Error3))
	server := &http.Server{
		Addr:    ":8080",
		Handler: router,
	}
	server.ListenAndServe()
}

run server

go run main.go

test response

$ curl -i http://127.0.0.1:8080/error1
HTTP/1.1 500 Internal Server Error
Content-Type: application/json; charset=utf-8
Date: Thu, 27 Feb 2020 09:39:56 GMT
Content-Length: 31

{"code":1000,"message":"test"}

$ curl -i http://127.0.0.1:8080/error2
HTTP/1.1 500 Internal Server Error
Content-Type: application/json; charset=utf-8
Date: Thu, 27 Feb 2020 09:40:04 GMT
Content-Length: 31

{"code":1001,"message":"test"}

$curl -i http://127.0.0.1:8080/error3
HTTP/1.1 400 Bad Request
Content-Type: application/json; charset=utf-8
Date: Thu, 27 Feb 2020 09:40:11 GMT
Content-Length: 31

{"code":1001,"message":"test"}

If possible I can submit a merge request to fix these issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.