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

@ -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)
}
}

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
}
}

View File

@ -3,7 +3,6 @@ package sync
import (
"bufio"
"encoding/hex"
"errors"
"fmt"
"path"
"strings"
@ -162,12 +161,12 @@ func ApiGetRemoteExerciceFiles(ps httprouter.Params, _ []byte) (interface{}, err
}
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"
@ -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"
@ -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 {

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"
@ -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
}