better error handling in c&w
This commit is contained in:
parent
64543d71d7
commit
020b55104f
@ -8,6 +8,7 @@ package mainline
|
|||||||
import (
|
import (
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/pkg/errors"
|
||||||
"net"
|
"net"
|
||||||
|
|
||||||
"github.com/anacrolix/missinggo/iter"
|
"github.com/anacrolix/missinggo/iter"
|
||||||
@ -251,10 +252,10 @@ func (e *Error) UnmarshalBencode(b []byte) (err error) {
|
|||||||
|
|
||||||
matches := result[0][1:]
|
matches := result[0][1:]
|
||||||
if _, err := fmt.Sscanf(string(matches[0]), "%d", &code); err != nil {
|
if _, err := fmt.Sscanf(string(matches[0]), "%d", &code); err != nil {
|
||||||
return fmt.Errorf("could not parse the error code: %s", err.Error())
|
return errors.Wrap(err, "could not parse error code")
|
||||||
}
|
}
|
||||||
if _, err := fmt.Sscanf(string(matches[1]), "%d", &msgLen); err != nil {
|
if _, err := fmt.Sscanf(string(matches[1]), "%d", &msgLen); err != nil {
|
||||||
return fmt.Errorf("could not parse the error message length: %s", err.Error())
|
return errors.Wrap(err, "could not parse error msg length")
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(matches[2]) != msgLen {
|
if len(matches[2]) != msgLen {
|
||||||
|
@ -2,6 +2,8 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
|
"fmt"
|
||||||
|
"github.com/pkg/errors"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@ -14,73 +16,67 @@ import (
|
|||||||
func rootHandler(w http.ResponseWriter, r *http.Request) {
|
func rootHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
nTorrents, err := database.GetNumberOfTorrents()
|
nTorrents, err := database.GetNumberOfTorrents()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err.Error())
|
handlerError(errors.Wrap(err, "GetNumberOfTorrents"), w)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = templates["homepage"].Execute(w, struct {
|
_ = templates["homepage"].Execute(w, struct {
|
||||||
NTorrents uint
|
NTorrents uint
|
||||||
}{
|
}{
|
||||||
NTorrents: nTorrents,
|
NTorrents: nTorrents,
|
||||||
})
|
})
|
||||||
if err != nil {
|
|
||||||
panic(err.Error())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: we might as well move torrents.html into static...
|
|
||||||
func torrentsHandler(w http.ResponseWriter, r *http.Request) {
|
func torrentsHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
data := mustAsset("templates/torrents.html")
|
data := mustAsset("templates/torrents.html")
|
||||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||||
// Cache static resources for a day
|
// Cache static resources for a day
|
||||||
w.Header().Set("Cache-Control", "max-age=86400")
|
w.Header().Set("Cache-Control", "max-age=86400")
|
||||||
w.Write(data)
|
_, _ = w.Write(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
func torrentsInfohashHandler(w http.ResponseWriter, r *http.Request) {
|
func torrentsInfohashHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
infoHash, err := hex.DecodeString(mux.Vars(r)["infohash"])
|
infoHash, err := hex.DecodeString(mux.Vars(r)["infohash"])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err.Error())
|
handlerError(errors.Wrap(err, "cannot decode infohash"), w)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
torrent, err := database.GetTorrent(infoHash)
|
torrent, err := database.GetTorrent(infoHash)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err.Error())
|
handlerError(errors.Wrap(err, "cannot get torrent"), w)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
if torrent == nil {
|
if torrent == nil {
|
||||||
w.WriteHeader(404)
|
respondError(w, http.StatusNotFound, "torrent not found!")
|
||||||
w.Write([]byte("torrent not found!"))
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
files, err := database.GetFiles(infoHash)
|
files, err := database.GetFiles(infoHash)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err.Error())
|
handlerError(errors.Wrap(err, "could not get files"), w)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
if files == nil {
|
if files == nil {
|
||||||
w.WriteHeader(500)
|
handlerError(fmt.Errorf("could not get files"), w)
|
||||||
w.Write([]byte("files not found what!!!"))
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = templates["torrent"].Execute(w, struct {
|
_ = templates["torrent"].Execute(w, struct {
|
||||||
T *persistence.TorrentMetadata
|
T *persistence.TorrentMetadata
|
||||||
F []persistence.File
|
F []persistence.File
|
||||||
}{
|
}{
|
||||||
T: torrent,
|
T: torrent,
|
||||||
F: files,
|
F: files,
|
||||||
})
|
})
|
||||||
if err != nil {
|
|
||||||
panic("error while executing template!")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: we might as well move statistics.html into static...
|
|
||||||
func statisticsHandler(w http.ResponseWriter, r *http.Request) {
|
func statisticsHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
data := mustAsset("templates/statistics.html")
|
data := mustAsset("templates/statistics.html")
|
||||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||||
// Cache static resources for a day
|
// Cache static resources for a day
|
||||||
w.Header().Set("Cache-Control", "max-age=86400")
|
w.Header().Set("Cache-Control", "max-age=86400")
|
||||||
w.Write(data)
|
_, _ = w.Write(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
func feedHandler(w http.ResponseWriter, r *http.Request) {
|
func feedHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
@ -111,32 +107,24 @@ func feedHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
nil,
|
nil,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
respondError(w, 400, err.Error())
|
handlerError(errors.Wrap(err, "query torrent"), w)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// It is much more convenient to write the XML deceleration manually*, and then process the XML
|
// It is much more convenient to write the XML deceleration manually*, and then process the XML
|
||||||
// template using template/html and send, then to use encoding/xml.
|
// template using template/html and send, than to use encoding/xml.
|
||||||
//
|
//
|
||||||
// *: https://github.com/golang/go/issues/3133
|
// *: https://github.com/golang/go/issues/3133
|
||||||
//
|
//
|
||||||
// TODO: maybe do it properly, even if it's inconvenient?
|
// TODO: maybe do it properly, even if it's inconvenient?
|
||||||
|
_, _ = w.Write([]byte(`<?xml version="1.0" encoding="utf-8" standalone="yes"?>`))
|
||||||
_, err = w.Write([]byte(`<?xml version="1.0" encoding="utf-8" standalone="yes"?>`))
|
_ = templates["feed"].Execute(w, struct {
|
||||||
if err != nil {
|
|
||||||
panic(err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
err = templates["feed"].Execute(w, struct {
|
|
||||||
Title string
|
Title string
|
||||||
Torrents []persistence.TorrentMetadata
|
Torrents []persistence.TorrentMetadata
|
||||||
}{
|
}{
|
||||||
Title: title,
|
Title: title,
|
||||||
Torrents: torrents,
|
Torrents: torrents,
|
||||||
})
|
})
|
||||||
if err != nil {
|
|
||||||
panic(err.Error())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func staticHandler(w http.ResponseWriter, r *http.Request) {
|
func staticHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
@ -155,5 +143,5 @@ func staticHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
w.Header().Set("Content-Type", contentType)
|
w.Header().Set("Content-Type", contentType)
|
||||||
// Cache static resources for a day
|
// Cache static resources for a day
|
||||||
w.Header().Set("Cache-Control", "max-age=86400")
|
w.Header().Set("Cache-Control", "max-age=86400")
|
||||||
w.Write(data)
|
_, _ = w.Write(data)
|
||||||
}
|
}
|
||||||
|
@ -5,9 +5,9 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/pkg/errors"
|
||||||
"html/template"
|
"html/template"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
@ -156,7 +156,7 @@ func main() {
|
|||||||
var err error
|
var err error
|
||||||
database, err = persistence.MakeDatabase(opts.Database, logger)
|
database, err = persistence.MakeDatabase(opts.Database, logger)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err.Error())
|
zap.L().Fatal("could not access to database", zap.Error(err))
|
||||||
}
|
}
|
||||||
|
|
||||||
decoder.IgnoreUnknownKeys(false)
|
decoder.IgnoreUnknownKeys(false)
|
||||||
@ -178,7 +178,8 @@ func respondError(w http.ResponseWriter, statusCode int, format string, a ...int
|
|||||||
func mustAsset(name string) []byte {
|
func mustAsset(name string) []byte {
|
||||||
data, err := Asset(name)
|
data, err := Asset(name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panicf("Could NOT access the requested resource `%s`: %s (please inform us, this is a BUG!)", name, err.Error())
|
zap.L().Panic("Could NOT access the requested resource! THIS IS A BUG, PLEASE REPORT",
|
||||||
|
zap.String("name", name), zap.Error(err))
|
||||||
}
|
}
|
||||||
return data
|
return data
|
||||||
}
|
}
|
||||||
@ -250,7 +251,7 @@ func loadCred(cred string) error {
|
|||||||
if err == io.EOF {
|
if err == io.EOF {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
return fmt.Errorf("error while reading line %d: %s", lineno, err.Error())
|
return errors.Wrapf(err, "while reading line %d", lineno)
|
||||||
}
|
}
|
||||||
|
|
||||||
line = line[:len(line)-1] // strip '\n'
|
line = line[:len(line)-1] // strip '\n'
|
||||||
@ -316,7 +317,5 @@ func BasicAuth(handler http.HandlerFunc, realm string) http.HandlerFunc {
|
|||||||
func authenticate(w http.ResponseWriter, realm string) {
|
func authenticate(w http.ResponseWriter, realm string) {
|
||||||
w.Header().Set("WWW-Authenticate", `Basic realm="`+realm+`"`)
|
w.Header().Set("WWW-Authenticate", `Basic realm="`+realm+`"`)
|
||||||
w.WriteHeader(401)
|
w.WriteHeader(401)
|
||||||
if _, err := w.Write([]byte("Unauthorised.\n")); err != nil {
|
_, _ = w.Write([]byte("Unauthorised.\n"))
|
||||||
panic(err.Error())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
8
cmd/magneticow/util.go
Normal file
8
cmd/magneticow/util.go
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "net/http"
|
||||||
|
|
||||||
|
func handlerError(err error, w http.ResponseWriter) {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
_, _ = w.Write([]byte(err.Error()))
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user