Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (cmd *FileCommandDelete) IsAuthorized(ctx context.Context, req *http.Reques
cmd.exisitingFileRef, _ = reference.GetReference(ctx, allocationObj.ID, path)

if cmd.exisitingFileRef == nil {
return common.NewError("invalid_file", "File does not exist at path")
return common.NewErrorfWithStatusCode(204, "invalid_file", "File does not exist at path")
}

return nil
Expand Down
10 changes: 8 additions & 2 deletions code/go/0chain.net/core/common/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import (

/*Error type for a new application error */
type Error struct {
Code string `json:"code,omitempty"`
Msg string `json:"msg"`
Code string `json:"code,omitempty"`
Msg string `json:"msg"`
StatusCode int `json:"status_code,omitempty"`
}

func (err *Error) Error() string {
Expand All @@ -24,6 +25,11 @@ func NewErrorf(code, format string, args ...interface{}) *Error {
return &Error{Code: code, Msg: fmt.Sprintf(format, args...)}
}

/*NewErrorf - create a new error with format */
func NewErrorfWithStatusCode(statusCode int, errCode, format string, args ...interface{}) *Error {
return &Error{StatusCode: statusCode, Code: errCode, Msg: fmt.Sprintf(format, args...)}
}

/*InvalidRequest - create error messages that are needed when validating request input */
func InvalidRequest(msg string) error {
return NewError("invalid_request", fmt.Sprintf("Invalid request (%v)", msg))
Expand Down
8 changes: 6 additions & 2 deletions code/go/0chain.net/core/common/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,18 @@ func ToByteStream(handler JSONResponderF) ReqRespHandlerf {
ctx := r.Context()
data, err := handler(ctx, r)
if err != nil {
statusCode := 400
if cerr, ok := err.(*Error); ok {
w.Header().Set(AppErrorHeader, cerr.Code)
if cerr.StatusCode != 0 {
statusCode = cerr.StatusCode
}
}
if data != nil {
responseString, _ := json.Marshal(data)
http.Error(w, string(responseString), 400)
http.Error(w, string(responseString), statusCode)
} else {
http.Error(w, err.Error(), 400)
http.Error(w, err.Error(), statusCode)
}
} else if data != nil {
rawdata, ok := data.([]byte)
Expand Down