Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move OCS error handlers to pkg #593

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions internal/http/services/owncloud/ocs/apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package ocs
import (
"net/http"

"github.com/cs3org/reva/pkg/errhandler"
"github.com/cs3org/reva/pkg/rhttp/router"
)

Expand Down Expand Up @@ -50,7 +51,7 @@ func (h *AppsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}
}
WriteOCSError(w, r, MetaNotFound.StatusCode, "Not found", nil)
errhandler.WriteError(w, r, errhandler.MetaNotFound.StatusCode, "Not found", nil)
case "notifications":
head, r.URL.Path = router.ShiftPath(r.URL.Path)
if head == "api" {
Expand All @@ -60,8 +61,8 @@ func (h *AppsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}
}
WriteOCSError(w, r, MetaNotFound.StatusCode, "Not found", nil)
errhandler.WriteError(w, r, errhandler.MetaNotFound.StatusCode, "Not found", nil)
default:
WriteOCSError(w, r, MetaNotFound.StatusCode, "Not found", nil)
errhandler.WriteError(w, r, errhandler.MetaNotFound.StatusCode, "Not found", nil)
}
}
4 changes: 3 additions & 1 deletion internal/http/services/owncloud/ocs/capabilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ package ocs
import (
"encoding/xml"
"net/http"

"github.com/cs3org/reva/pkg/errhandler"
)

// ocsBool implements the xml/json Marshaler interface. The OCS API inconsistency require us to parse boolean values
Expand Down Expand Up @@ -231,7 +233,7 @@ func (h *CapabilitiesHandler) init(c *Config) {
// Handler renders the capabilities
func (h *CapabilitiesHandler) Handler() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
WriteOCSSuccess(w, r, h.c)
errhandler.WriteSuccess(w, r, h.c)
})
}

Expand Down
3 changes: 2 additions & 1 deletion internal/http/services/owncloud/ocs/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package ocs
import (
"net/http"

"github.com/cs3org/reva/pkg/errhandler"
"github.com/cs3org/reva/pkg/rhttp/router"
)

Expand Down Expand Up @@ -51,7 +52,7 @@ func (h *CloudHandler) Handler() http.Handler {
case "users":
h.UsersHandler.ServeHTTP(w, r)
default:
WriteOCSError(w, r, MetaNotFound.StatusCode, "Not found", nil)
errhandler.WriteError(w, r, errhandler.MetaNotFound.StatusCode, "Not found", nil)
}
})
}
4 changes: 3 additions & 1 deletion internal/http/services/owncloud/ocs/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ package ocs

import (
"net/http"

"github.com/cs3org/reva/pkg/errhandler"
)

// ConfigHandler renders the config endpoint
Expand Down Expand Up @@ -50,7 +52,7 @@ func (h *ConfigHandler) init(c *Config) {
// Handler renders the config
func (h *ConfigHandler) Handler() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
WriteOCSSuccess(w, r, h.c)
errhandler.WriteSuccess(w, r, h.c)
})
}

Expand Down
3 changes: 2 additions & 1 deletion internal/http/services/owncloud/ocs/ocs.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"net/http"

"github.com/cs3org/reva/pkg/appctx"
"github.com/cs3org/reva/pkg/errhandler"
"github.com/cs3org/reva/pkg/rhttp/global"
"github.com/cs3org/reva/pkg/rhttp/router"
"github.com/cs3org/reva/pkg/sharedconf"
Expand Down Expand Up @@ -99,6 +100,6 @@ func (s *svc) Handler() http.Handler {
return
}

WriteOCSError(w, r, MetaNotFound.StatusCode, "Not found", nil)
errhandler.WriteError(w, r, errhandler.MetaNotFound.StatusCode, "Not found", nil)
})
}
126 changes: 1 addition & 125 deletions internal/http/services/owncloud/ocs/reqres.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,131 +18,7 @@

package ocs

import (
"encoding/json"
"encoding/xml"
"net/http"

user "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1"
"github.com/cs3org/reva/pkg/appctx"
)

// Response is the top level response structure
type Response struct {
OCS *Payload `json:"ocs"`
}

// Payload combines response metadata and data
type Payload struct {
XMLName struct{} `json:"-" xml:"ocs"`
Meta *ResponseMeta `json:"meta" xml:"meta"`
Data interface{} `json:"data,omitempty" xml:"data,omitempty"`
}

// ResponseMeta holds response metadata
type ResponseMeta struct {
Status string `json:"status" xml:"status"`
StatusCode int `json:"statuscode" xml:"statuscode"`
Message string `json:"message" xml:"message"`
TotalItems string `json:"totalitems,omitempty" xml:"totalitems,omitempty"`
ItemsPerPage string `json:"itemsperpage,omitempty" xml:"itemsperpage,omitempty"`
}

// MetaOK is the default ok response
var MetaOK = &ResponseMeta{Status: "ok", StatusCode: 100, Message: "OK"}

// MetaBadRequest is used for unknown errers
var MetaBadRequest = &ResponseMeta{Status: "error", StatusCode: 400, Message: "Bad Request"}

// MetaServerError is returned on server errors
var MetaServerError = &ResponseMeta{Status: "error", StatusCode: 996, Message: "Server Error"}

// MetaUnauthorized is returned on unauthorized requests
var MetaUnauthorized = &ResponseMeta{Status: "error", StatusCode: 997, Message: "Unauthorised"}

// MetaNotFound is returned when trying to access not existing resources
var MetaNotFound = &ResponseMeta{Status: "error", StatusCode: 998, Message: "Not Found"}

// MetaUnknownError is used for unknown errers
var MetaUnknownError = &ResponseMeta{Status: "error", StatusCode: 999, Message: "Unknown Error"}

// WriteOCSSuccess handles writing successful ocs response data
func WriteOCSSuccess(w http.ResponseWriter, r *http.Request, d interface{}) {
WriteOCSData(w, r, MetaOK, d, nil)
}

// WriteOCSError handles writing error ocs responses
func WriteOCSError(w http.ResponseWriter, r *http.Request, c int, m string, err error) {
WriteOCSData(w, r, &ResponseMeta{Status: "error", StatusCode: c, Message: m}, nil, err)
}

// WriteOCSData handles writing ocs data in json and xml
func WriteOCSData(w http.ResponseWriter, r *http.Request, m *ResponseMeta, d interface{}, err error) {
WriteOCSResponse(w, r, &Response{
OCS: &Payload{
Meta: m,
Data: d,
},
}, err)
}

// WriteOCSResponse handles writing ocs responses in json and xml
func WriteOCSResponse(w http.ResponseWriter, r *http.Request, res *Response, err error) {
var encoded []byte

if err != nil {
appctx.GetLogger(r.Context()).Error().Err(err).Msg(res.OCS.Meta.Message)
}

if r.URL.Query().Get("format") == "json" {
w.Header().Set("Content-Type", "application/json")
encoded, err = json.Marshal(res)
} else {
w.Header().Set("Content-Type", "application/xml")
_, err = w.Write([]byte(xml.Header))
if err != nil {
appctx.GetLogger(r.Context()).Error().Err(err).Msg("error writing xml header")
w.WriteHeader(http.StatusInternalServerError)
return
}
encoded, err = xml.Marshal(res.OCS)
}
if err != nil {
appctx.GetLogger(r.Context()).Error().Err(err).Msg("error encoding ocs response")
w.WriteHeader(http.StatusInternalServerError)
return
}

// TODO map error for v2 only?
// see https://github.com/owncloud/core/commit/bacf1603ffd53b7a5f73854d1d0ceb4ae545ce9f#diff-262cbf0df26b45bad0cf00d947345d9c
switch res.OCS.Meta.StatusCode {
case MetaNotFound.StatusCode:
w.WriteHeader(http.StatusNotFound)
case MetaServerError.StatusCode:
w.WriteHeader(http.StatusInternalServerError)
case MetaUnknownError.StatusCode:
w.WriteHeader(http.StatusInternalServerError)
case MetaUnauthorized.StatusCode:
w.WriteHeader(http.StatusUnauthorized)
case 100:
w.WriteHeader(http.StatusOK)
case 104:
w.WriteHeader(http.StatusForbidden)
default:
// any 2xx, 4xx and 5xx will be used as is
if res.OCS.Meta.StatusCode >= 200 && res.OCS.Meta.StatusCode < 600 {
w.WriteHeader(res.OCS.Meta.StatusCode)
} else {
w.WriteHeader(http.StatusBadRequest)
}
}

_, err = w.Write(encoded)
if err != nil {
appctx.GetLogger(r.Context()).Error().Err(err).Msg("error writing ocs response")
w.WriteHeader(http.StatusInternalServerError)
}
}
import user "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1"

// UserIDToString returns a userid string with an optional idp separated by @: "<opaque id>[@<idp>]"
func UserIDToString(userID *user.UserId) string {
Expand Down
Loading