responderror auto sprintf

This commit is contained in:
sentriz
2019-05-28 15:07:58 +01:00
parent 74c55bd509
commit e7ed19abd9
5 changed files with 13 additions and 18 deletions

View File

@@ -1,7 +1,6 @@
package handler
import (
"fmt"
"net/http"
"github.com/jinzhu/gorm"
@@ -126,9 +125,8 @@ func (c *Controller) GetAlbumList(w http.ResponseWriter, r *http.Request) {
user.ID)
q = q.Order("plays.time DESC")
default:
respondError(w, r, 10, fmt.Sprintf(
"unknown value `%s` for parameter 'type'", listType,
))
respondError(w, r, 10,
"unknown value `%s` for parameter 'type'", listType)
return
}
var folders []model.Folder

View File

@@ -1,7 +1,6 @@
package handler
import (
"fmt"
"net/http"
"github.com/jinzhu/gorm"
@@ -122,9 +121,8 @@ func (c *Controller) GetAlbumListTwo(w http.ResponseWriter, r *http.Request) {
user.ID)
q = q.Order("plays.time DESC")
default:
respondError(w, r, 10, fmt.Sprintf(
"unknown value `%s` for parameter 'type'", listType,
))
respondError(w, r, 10,
"unknown value `%s` for parameter 'type'", listType)
return
}
var albums []model.Album

View File

@@ -1,7 +1,6 @@
package handler
import (
"fmt"
"net/http"
"os"
"path"
@@ -38,13 +37,13 @@ func (c *Controller) Stream(w http.ResponseWriter, r *http.Request) {
Preload("Folder").
First(&track, id)
if track.Path == "" {
respondError(w, r, 70, fmt.Sprintf("media with id `%d` was not found", id))
respondError(w, r, 70, "media with id `%d` was not found", id)
return
}
absPath := path.Join(c.MusicPath, track.Path)
file, err := os.Open(absPath)
if err != nil {
respondError(w, r, 0, fmt.Sprintf("error while streaming media: %v", err))
respondError(w, r, 0, "error while streaming media: %v", err)
return
}
stat, _ := file.Stat()
@@ -117,7 +116,7 @@ func (c *Controller) Scrobble(w http.ResponseWriter, r *http.Request) {
getStrParamOr(r, "submission", "true") != "false",
)
if err != nil {
respondError(w, r, 0, fmt.Sprintf("error when submitting: %v", err))
respondError(w, r, 0, "error when submitting: %v", err)
return
}
sub := subsonic.NewResponse()

View File

@@ -55,14 +55,13 @@ func (c *Controller) WithValidSubsonicArgs(next http.HandlerFunc) http.HandlerFu
passwordAuth, tokenAuth := token == "" && salt == "",
password == ""
if tokenAuth == passwordAuth {
respondError(w, r,
10, "please provide parameters `t` and `s`, or just `p`",
)
respondError(w, r, 10,
"please provide parameters `t` and `s`, or just `p`")
return
}
user := c.GetUserFromName(username)
if user == nil {
respondError(w, r, 40, "invalid username")
respondError(w, r, 40, "invalid username `%s`", username)
return
}
var credsOk bool

View File

@@ -3,6 +3,7 @@ package handler
import (
"encoding/json"
"encoding/xml"
"fmt"
"log"
"net/http"
@@ -49,8 +50,8 @@ func respond(w http.ResponseWriter, r *http.Request,
}
func respondError(w http.ResponseWriter, r *http.Request,
code int, message string) {
code int, message string, a ...interface{}) {
respondRaw(w, r, http.StatusBadRequest, subsonic.NewError(
code, message,
code, fmt.Sprintf(message, a...),
))
}