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
}

View File

@ -2,7 +2,6 @@ package main
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
@ -103,7 +102,7 @@ func genTeamIssuesFile(team fic.Team) error {
if s, err := os.Stat(dirPath); os.IsNotExist(err) {
os.MkdirAll(dirPath, 0777)
} else if !s.IsDir() {
return errors.New(fmt.Sprintf("%s is not a directory", dirPath))
return fmt.Errorf("%s is not a directory", dirPath)
}
if my, err := team.MyIssueFile(); err != nil {
@ -124,7 +123,7 @@ func genTeamMyFile(team *fic.Team) error {
if s, err := os.Stat(dirPath); os.IsNotExist(err) {
os.MkdirAll(dirPath, 0777)
} else if !s.IsDir() {
return errors.New(fmt.Sprintf("%s is not a directory", dirPath))
return fmt.Errorf("%s is not a directory", dirPath)
}
if my, err := fic.MyJSONTeam(team, true); err != nil {
@ -156,7 +155,7 @@ func genMyPublicFile() error {
if s, err := os.Stat(dirPath); os.IsNotExist(err) {
os.MkdirAll(dirPath, 0777)
} else if !s.IsDir() {
return errors.New(fmt.Sprintf("%s is not a directory", dirPath))
return fmt.Errorf("%s is not a directory", dirPath)
}
if my, err := fic.MyJSONTeam(nil, true); err != nil {
@ -237,7 +236,7 @@ func genAll() {
log.Println("Team retrieval error: ", err)
} else {
for _, team := range teams {
myteam := team // team is reused, we need to create a new variable here to store the value
myteam := team // team is reused, we need to create a new variable here to store the value
genTeamQueue <- &myteam
}
}

View File

@ -210,7 +210,7 @@ func (k FlagKey) AddDepend(j Flag) (err error) {
} else if d, ok := j.(MCQ); ok {
_, err = DBExec("INSERT INTO exercice_flags_omcq_deps (id_flag, id_mcq_dep) VALUES (?, ?)", k.Id, d.Id)
} else {
err = errors.New(fmt.Sprintf("Dependancy type for key (%T) not implemented for this flag.", j))
err = fmt.Errorf("Dependancy type for key (%T) not implemented for this flag.", j)
}
return
}

View File

@ -1,7 +1,6 @@
package fic
import (
"errors"
"fmt"
"path"
"strings"
@ -12,18 +11,18 @@ var HintCoefficient = 1.0
// EHint represents a challenge hint.
type EHint struct {
Id int64 `json:"id"`
Id int64 `json:"id"`
// IdExercice is the identifier of the underlying challenge
IdExercice int64 `json:"idExercice"`
IdExercice int64 `json:"idExercice"`
// Title is the hint name displayed to players
Title string `json:"title"`
Title string `json:"title"`
// Content is the actual content of small text hints (mutually exclusive with File field)
// When File is filled, Content contains the hexadecimal file's hash.
Content string `json:"content"`
Content string `json:"content"`
// File is path, relative to FilesDir where the file hint is stored (mutually exclusive with Content field)
File string `json:"file"`
File string `json:"file"`
// Cost is the amount of points the player will loose if it unlocks the hint
Cost int64 `json:"cost"`
Cost int64 `json:"cost"`
}
// treatHintContent reads Content to detect if this is a hint file in order to convert to such hint.
@ -138,7 +137,7 @@ func (h EHint) AddDepend(f Flag) (err error) {
} else if d, ok := f.(MCQ); ok {
_, err = DBExec("INSERT INTO exercice_hints_omcq_deps (id_hint, id_mcq_dep) VALUES (?, ?)", h.Id, d.Id)
} else {
err = errors.New(fmt.Sprintf("Dependancy type for key (%T) not implemented for this flag.", f))
err = fmt.Errorf("Dependancy type for key (%T) not implemented for this flag.", f)
}
return
}