API resiolution with error handling.

This commit is contained in:
Miroslav Šedivý 2020-10-30 19:56:22 +01:00
parent 0c40eda36b
commit d48b857dfe
2 changed files with 81 additions and 27 deletions

View File

@ -4,12 +4,9 @@ import (
"net/http"
"github.com/go-chi/render"
)
type ErrResponse struct {
Code int `json:"code"`
Message string `json:"message"`
}
"demodesk/neko/internal/api/utils"
)
type ResolutionStruct struct {
Width int `json:"width"`
@ -17,15 +14,15 @@ type ResolutionStruct struct {
Rate int `json:"rate"`
}
func (a *ResolutionStruct) Bind(r *http.Request) error {
return nil
}
func (h *RoomHandler) ResolutionGet(w http.ResponseWriter, r *http.Request) {
size := h.remote.GetScreenSize()
if size == nil {
w.WriteHeader(http.StatusInternalServerError)
render.JSON(w, r, ErrResponse{
Code: -1,
Message: "Unable to get current screen resolution.",
})
render.Render(w, r, utils.ErrMessage(500, "Not implmented."))
return
}
@ -37,25 +34,22 @@ func (h *RoomHandler) ResolutionGet(w http.ResponseWriter, r *http.Request) {
}
func (h *RoomHandler) ResolutionChange(w http.ResponseWriter, r *http.Request) {
// data := &ResolutionStruct{}
// if err := render.Bind(r, data); err != nil {
// render.JSON(w, r, ErrResponse{
// Code: -1,
// Message: "Invalid Request.",
// })
// return
// }
data := &ResolutionStruct{}
if err := render.Bind(r, data); err != nil {
render.Render(w, r, utils.ErrInvalidRequest(err))
return
}
render.JSON(w, r, ErrResponse{
Code: -1,
Message: "Not implmented.",
})
if err := h.remote.ChangeResolution(data.Width, data.Height, data.Rate); err != nil {
render.Render(w, r, utils.ErrInvalidRequest(err))
return
}
// TODO: WebSocket notify.
render.JSON(w, r, data)
}
func (h *RoomHandler) ResolutionList(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
render.JSON(w, r, ErrResponse{
Code: -1,
Message: "Not implmented.",
})
render.Render(w, r, utils.ErrMessage(500, "Not implmented."))
}

View File

@ -0,0 +1,60 @@
package utils
import (
"net/http"
"github.com/go-chi/render"
)
//--
// Error response payloads & renderers
//--
// ErrResponse renderer type for handling all sorts of errors.
//
// In the best case scenario, the excellent github.com/pkg/errors package
// helps reveal information on the error, setting it on Err, and in the Render()
// method, using it to set the application-specific error code in AppCode.
type ErrResponse struct {
Err error `json:"-"` // low-level runtime error
HTTPStatusCode int `json:"-"` // http response status code
StatusText string `json:"status"` // user-level status message
AppCode int64 `json:"code,omitempty"` // application-specific error code
ErrorText string `json:"error,omitempty"` // application-level error message, for debugging
}
func (e *ErrResponse) Render(w http.ResponseWriter, r *http.Request) error {
render.Status(r, e.HTTPStatusCode)
return nil
}
func ErrMessage(HTTPStatusCode int, StatusText string) render.Renderer {
return &ErrResponse{
HTTPStatusCode: HTTPStatusCode,
StatusText: StatusText,
}
}
func ErrInvalidRequest(err error) render.Renderer {
return &ErrResponse{
Err: err,
HTTPStatusCode: 400,
StatusText: "Invalid request.",
ErrorText: err.Error(),
}
}
func ErrRender(err error) render.Renderer {
return &ErrResponse{
Err: err,
HTTPStatusCode: 422,
StatusText: "Error rendering response.",
ErrorText: err.Error(),
}
}
var ErrNotFound = &ErrResponse{
HTTPStatusCode: 404,
StatusText: "Resource not found.",
}