Use fmt.Errorf

This commit is contained in:
nemunaire 2020-04-15 07:39:38 +02:00
parent 45069d4fbb
commit adb424ea03
11 changed files with 50 additions and 60 deletions

View file

@ -118,7 +118,7 @@ func exportResolutionMovies(_ httprouter.Params, body []byte) (interface{}, erro
}
}
func loadFlags(n func () ([]fic.Flag, error)) (interface{}, error) {
func loadFlags(n func() ([]fic.Flag, error)) (interface{}, error) {
if flags, err := n(); err != nil {
return nil, err
} else {
@ -138,7 +138,7 @@ func loadFlags(n func () ([]fic.Flag, error)) (interface{}, error) {
ret = append(ret, m)
}
} else {
return nil, errors.New(fmt.Sprintf("Flag type %T not implemented for this flag.", f))
return nil, fmt.Errorf("Flag type %T not implemented for this flag.", f)
}
}
@ -181,11 +181,11 @@ type exerciceStats struct {
func getExerciceStats(e fic.Exercice, body []byte) (interface{}, error) {
return exerciceStats{
TeamTries: e.TriedTeamCount(),
TotalTries: e.TriedCount(),
TeamTries: e.TriedTeamCount(),
TotalTries: e.TriedCount(),
SolvedCount: e.SolvedCount(),
FlagSolved: e.FlagSolved(),
MCQSolved: e.MCQSolved(),
FlagSolved: e.FlagSolved(),
MCQSolved: e.MCQSolved(),
}, nil
}
@ -196,12 +196,12 @@ func getExercicesStats(_ httprouter.Params, body []byte) (interface{}, error) {
ret := []exerciceStats{}
for _, e := range exercices {
ret = append(ret, exerciceStats{
IdExercice: e.Id,
TeamTries: e.TriedTeamCount(),
TotalTries: e.TriedCount(),
IdExercice: e.Id,
TeamTries: e.TriedTeamCount(),
TotalTries: e.TriedCount(),
SolvedCount: e.SolvedCount(),
FlagSolved: e.FlagSolved(),
MCQSolved: e.MCQSolved(),
FlagSolved: e.FlagSolved(),
MCQSolved: e.MCQSolved(),
})
}
return ret, nil
@ -406,7 +406,7 @@ type uploadedFlag struct {
ValidatorRe *string `json:"validator_regexp"`
Flag string
Value []byte
ChoicesCost int64 `json:"choices_cost"`
ChoicesCost int64 `json:"choices_cost"`
}
func createExerciceFlag(exercice fic.Exercice, body []byte) (interface{}, error) {

View file

@ -3,7 +3,6 @@ package api
import (
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"srs.epita.fr/fic-server/admin/sync"
@ -77,7 +76,7 @@ func genFileList(in []fic.EFile, e error) (out []APIFile, err error) {
g.Depends = append(g.Depends, m)
} else {
err = errors.New(fmt.Sprintf("Unknown type %T to handle file dependancy", k))
err = fmt.Errorf("Unknown type %T to handle file dependancy", k)
return
}
}
@ -148,6 +147,6 @@ func deleteFileDep(file fic.EFile, depid int64, _ []byte) (interface{}, error) {
return true, file.DeleteDepend(fic.FlagKey{Id: depid})
}
func checkFile(file fic.EFile, _ []byte) (interface{}, error) {
func checkFile(file fic.EFile, _ []byte) (interface{}, error) {
return true, file.CheckFileOnDisk()
}

View file

@ -3,7 +3,6 @@ package sync
import (
"bufio"
"encoding/hex"
"errors"
"fmt"
"path"
"strings"
@ -154,20 +153,20 @@ func ApiGetRemoteExerciceFiles(ps httprouter.Params, _ []byte) (interface{}, err
fPath := path.Join(exercice.Path, "files", fname)
fSize, _ := getFileSize(GlobalImporter, fPath)
ret = append(ret, fic.EFile{
Path: fPath,
Name: fname,
Path: fPath,
Name: fname,
Checksum: digests[fname],
Size: fSize,
Size: fSize,
})
}
return ret, nil
} else {
return nil, errors.New(fmt.Sprintf("%q", errs))
return nil, fmt.Errorf("%q", errs)
}
} else {
return nil, errors.New(fmt.Sprintf("%q", errs))
return nil, fmt.Errorf("%q", errs)
}
} else {
return nil, errors.New(fmt.Sprintf("%q", errs))
return nil, fmt.Errorf("%q", errs)
}
}

View file

@ -4,7 +4,6 @@ import (
"bufio"
"crypto"
"encoding/hex"
"errors"
"fmt"
"io"
"os"
@ -150,12 +149,12 @@ func ApiGetRemoteExerciceHints(ps httprouter.Params, _ []byte) (interface{}, err
if hints != nil {
return hints, nil
} else {
return hints, errors.New(fmt.Sprintf("%q", errs))
return hints, fmt.Errorf("%q", errs)
}
} else {
return exercice, errors.New(fmt.Sprintf("%q", errs))
return exercice, fmt.Errorf("%q", errs)
}
} else {
return nil, errors.New(fmt.Sprintf("%q", errs))
return nil, fmt.Errorf("%q", errs)
}
}

View file

@ -1,7 +1,6 @@
package sync
import (
"errors"
"fmt"
"math/rand"
"path"
@ -282,8 +281,8 @@ func buildExerciceFlag(i Importer, exercice fic.Exercice, flag ExerciceFlag, nli
}
ret = append(ret, importFlag{
Line: nline + 1,
Flag: addedFlag,
Line: nline + 1,
Flag: addedFlag,
})
}
return
@ -392,7 +391,6 @@ func ExerciceFlagsMap(i Importer, exercice fic.Exercice) (kmap map[int64]fic.Fla
return
}
// SyncExerciceFlags imports all kind of flags for the given challenge.
func SyncExerciceFlags(i Importer, exercice fic.Exercice) (kmap map[int64]fic.Flag, errs []string) {
if _, err := exercice.WipeFlags(); err != nil {
@ -456,12 +454,12 @@ func ApiGetRemoteExerciceFlags(ps httprouter.Params, _ []byte) (interface{}, err
if flags != nil {
return flags, nil
} else {
return flags, errors.New(fmt.Sprintf("%q", errs))
return flags, fmt.Errorf("%q", errs)
}
} else {
return exercice, errors.New(fmt.Sprintf("%q", errs))
return exercice, fmt.Errorf("%q", errs)
}
} else {
return nil, errors.New(fmt.Sprintf("%q", errs))
return nil, fmt.Errorf("%q", errs)
}
}

View file

@ -1,7 +1,6 @@
package sync
import (
"errors"
"fmt"
"path"
"strconv"
@ -71,13 +70,13 @@ func buildDependancyMap(i Importer, theme fic.Theme) (dmap map[int64]fic.Exercic
func parseExerciceDirname(edir string) (eid int, ename string, err error) {
edir_splt := strings.SplitN(edir, "-", 2)
if len(edir_splt) != 2 {
err = errors.New(fmt.Sprintf("%q is not a valid exercice directory: missing id prefix", edir))
err = fmt.Errorf("%q is not a valid exercice directory: missing id prefix", edir)
return
}
eid, err = strconv.Atoi(edir_splt[0])
if err != nil {
err = errors.New(fmt.Sprintf("%q: invalid exercice identifier: %s", edir, err))
err = fmt.Errorf("%q: invalid exercice identifier: %s", edir, err)
return
}
@ -270,7 +269,7 @@ func ApiListRemoteExercices(ps httprouter.Params, _ []byte) (interface{}, error)
if theme != nil {
return GetExercices(GlobalImporter, *theme)
} else {
return nil, errors.New(fmt.Sprintf("%q", errs))
return nil, fmt.Errorf("%q", errs)
}
}
@ -282,9 +281,9 @@ func ApiGetRemoteExercice(ps httprouter.Params, _ []byte) (interface{}, error) {
if exercice != nil {
return exercice, nil
} else {
return exercice, errors.New(fmt.Sprintf("%q", errs))
return exercice, fmt.Errorf("%q", errs)
}
} else {
return nil, errors.New(fmt.Sprintf("%q", errs))
return nil, fmt.Errorf("%q", errs)
}
}

View file

@ -4,7 +4,6 @@ import (
"bufio"
"bytes"
"encoding/base32"
"errors"
"fmt"
"io"
"os"
@ -75,7 +74,7 @@ func getFileSize(i Importer, URI string) (size int64, err error) {
}
}
return size, errors.New(fmt.Sprintf("%q: no such file or directory", URI))
return size, fmt.Errorf("%q: no such file or directory", URI)
}
// getFile helps to manage huge file transfert by concatenating splitted (with split(1)) files.
@ -110,7 +109,7 @@ func getFile(i Importer, URI string, writer *bufio.Writer) error {
}
}
return errors.New(fmt.Sprintf("%q: no such file or directory", URI))
return fmt.Errorf("%q: no such file or directory", URI)
}
// getFileContent retrieves the content of the given text file.

View file

@ -1,7 +1,6 @@
package sync
import (
"errors"
"fmt"
"image"
"image/jpeg"
@ -183,8 +182,8 @@ func SyncThemes(i Importer) []string {
btheme.Image = strings.TrimPrefix(filePath, fic.FilesDir)
return nil, nil
}); err != nil {
errs = append(errs, fmt.Sprintf("%q: unable to import heading image: %s", tdir, err))
}
errs = append(errs, fmt.Sprintf("%q: unable to import heading image: %s", tdir, err))
}
}
var theme fic.Theme
@ -217,7 +216,7 @@ func ApiListRemoteThemes(_ httprouter.Params, _ []byte) (interface{}, error) {
func ApiGetRemoteTheme(ps httprouter.Params, _ []byte) (interface{}, error) {
r, errs := BuildTheme(GlobalImporter, ps.ByName("thid"))
if r == nil {
return r, errors.New(fmt.Sprintf("%q", errs))
return r, fmt.Errorf("%q", errs)
} else {
return r, nil
}