当前位置: 首页>>代码示例>>Golang>>正文


Golang ResponseWriter.FromError方法代码示例

本文整理汇总了Golang中net/http.ResponseWriter.FromError方法的典型用法代码示例。如果您正苦于以下问题:Golang ResponseWriter.FromError方法的具体用法?Golang ResponseWriter.FromError怎么用?Golang ResponseWriter.FromError使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在net/http.ResponseWriter的用法示例。


在下文中一共展示了ResponseWriter.FromError方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: ServeHTTP

func (h *resourceHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
	var (
		ctx          *context
		in, out      interface{}
		err          error
		status       int
		responseType string
	)

	// Recover from panics by sending an HTTP error.
	defer func() {
		if recovered := recover(); recovered != nil {
			response := restdata.ErrorResponse{}
			response.FromPanic(recovered)
			writeAResponse(resp, http.StatusInternalServerError, restdata.V1JSONMediaType, response, toJSON)
		}
	}()

	// Start by trying to come up with a response type, even before
	// trying to parse the input.  This determines what format an
	// error message could be sent back as.
	if err == nil {
		// Errors here by default are in the header setup
		status = http.StatusBadRequest
		responseType, err = negotiateResponse(req)
		if err != nil {
			// Gotta pick something
			responseType = restdata.V1JSONMediaType
		}
	}

	// Get bits from URL parameters
	if err == nil {
		ctx, err = h.Context(req)
	}

	// Read the (JSON?) body, if it's there
	if err == nil && (req.Method == "PUT" || req.Method == "POST") {
		// Make a new object of the same type as h.In
		in = reflect.Zero(reflect.TypeOf(h.Representation)).Interface()

		// Then decode the message body into that object
		contentType := req.Header.Get("Content-Type")
		err = restdata.Decode(contentType, req.Body, &in)
	}

	// Actually call the handler method
	if err == nil {
		// We will return this if the method is unexpected or
		// we don't have a handler for it
		err = errMethodNotAllowed{Method: req.Method}
		// If anything else goes wrong here, it's an error in
		// client code
		status = http.StatusInternalServerError
		switch req.Method {
		case "GET", "HEAD":
			if h.Get != nil {
				out, err = h.Get(ctx)
			}
		case "PUT":
			if h.Put != nil {
				out, err = h.Put(ctx, in)
			}
		case "POST":
			if h.Post != nil {
				out, err = h.Post(ctx, in)
			}
		case "DELETE":
			if h.Delete != nil {
				out, err = h.Delete(ctx)
			}
		}
	}

	// Fix up the final result based on what we know.
	if err != nil {
		// Pick a better status code if we know of one
		if errS, hasStatus := err.(restdata.ErrorStatus); hasStatus {
			status = errS.HTTPStatus()
		}
		resp := restdata.ErrorResponse{Error: "error", Message: err.Error()}
		resp.FromError(err)
		// Remap well-known coordinate errors
		out = resp
	} else if out == nil {
		status = http.StatusNoContent
	} else if created, isCreated := out.(responseCreated); isCreated {
		status = http.StatusCreated
		if created.Location != "" {
			resp.Header().Set("Location", created.Location)
		}
		if req.Method == "HEAD" {
			out = nil
		} else {
			out = created.Body
		}
	} else {
		status = http.StatusOK
		if req.Method == "HEAD" {
			out = nil
//.........这里部分代码省略.........
开发者ID:diffeo,项目名称:go-coordinate,代码行数:101,代码来源:rest.go


注:本文中的net/http.ResponseWriter.FromError方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。