Skip to content

Commit

Permalink
fix: Error wrapping
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Boutour <[email protected]>
  • Loading branch information
ViBiOh committed Aug 16, 2022
1 parent 9721b8b commit f5c2682
Show file tree
Hide file tree
Showing 30 changed files with 90 additions and 90 deletions.
2 changes: 1 addition & 1 deletion pkg/crud/bcrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func findBcryptBestCost(maxDuration time.Duration) (int, error) {
for i := bcrypt.MinCost + 1; i <= bcrypt.MaxCost; i++ {
hashedPassword, err := bcrypt.GenerateFromPassword(password, i)
if err != nil {
return i, fmt.Errorf("generate password: %s", err)
return i, fmt.Errorf("generate password: %w", err)
}

start := time.Now()
Expand Down
4 changes: 2 additions & 2 deletions pkg/crud/crud.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func New(config Config, storage absto.Storage, rendererApp renderer.App, shareAp

if amqpClient != nil {
if err := amqpClient.SetupExclusive(app.amqpExclusiveRoutingKey); err != nil {
return app, fmt.Errorf("setup amqp exclusive: %s", err)
return app, fmt.Errorf("setup amqp exclusive: %w", err)
}
}

Expand Down Expand Up @@ -125,7 +125,7 @@ func New(config Config, storage absto.Storage, rendererApp renderer.App, shareAp

bcryptDuration, err := time.ParseDuration(strings.TrimSpace(*config.bcryptDuration))
if err != nil {
return app, fmt.Errorf("parse bcrypt duration: %s", err)
return app, fmt.Errorf("parse bcrypt duration: %w", err)
}

bcryptCost, err := findBcryptBestCost(bcryptDuration)
Expand Down
4 changes: 2 additions & 2 deletions pkg/crud/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func (a App) zipItems(ctx context.Context, done <-chan struct{}, request provide
var nestedItems []absto.Item
nestedItems, err = a.storageApp.List(ctx, request.SubPath(relativeURL))
if err != nil {
err = fmt.Errorf("zip nested folder `%s`: %s", relativeURL, err)
err = fmt.Errorf("zip nested folder `%s`: %w", relativeURL, err)
return
}

Expand All @@ -174,7 +174,7 @@ func (a App) addFileToZip(ctx context.Context, zipWriter *zip.Writer, item absto
var writer io.Writer
writer, err = zipWriter.CreateHeader(header)
if err != nil {
return fmt.Errorf("create zip header: %s", err)
return fmt.Errorf("create zip header: %w", err)
}

var reader io.ReadCloser
Expand Down
8 changes: 4 additions & 4 deletions pkg/crud/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func parseMultipart(r *http.Request) (map[string]string, *multipart.Part, error)
}

if err != nil {
return nil, nil, fmt.Errorf("error while reader multipart: %s", err)
return nil, nil, fmt.Errorf("error while reader multipart: %w", err)
}

formName := part.FormName()
Expand All @@ -41,7 +41,7 @@ func parseMultipart(r *http.Request) (map[string]string, *multipart.Part, error)
default:
value, err := io.ReadAll(part)
if err != nil {
return nil, nil, fmt.Errorf("read form value `%s`: %s", formName, err)
return nil, nil, fmt.Errorf("read form value `%s`: %w", formName, err)
}

values[formName] = string(value)
Expand Down Expand Up @@ -88,7 +88,7 @@ func (a App) handleMultipart(w http.ResponseWriter, r *http.Request, request pro

values, file, err := parseMultipart(r)
if err != nil {
a.error(w, r, request, model.WrapInternal(fmt.Errorf("parse multipart request: %s", err)))
a.error(w, r, request, model.WrapInternal(fmt.Errorf("parse multipart request: %w", err)))
return
}

Expand All @@ -101,7 +101,7 @@ func (a App) handleMultipart(w http.ResponseWriter, r *http.Request, request pro
if chunkNumber := r.Header.Get("X-Chunk-Number"); len(chunkNumber) != 0 {
chunkNumberValue, err := strconv.ParseUint(chunkNumber, 10, 64)
if err != nil {
a.error(w, r, request, model.WrapInvalid(fmt.Errorf("parse chunk number: %s", err)))
a.error(w, r, request, model.WrapInvalid(fmt.Errorf("parse chunk number: %w", err)))
}

chunkNumber = fmt.Sprintf("%10d", chunkNumberValue)
Expand Down
6 changes: 3 additions & 3 deletions pkg/crud/rename.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,18 +160,18 @@ func (a App) checkFile(ctx context.Context, pathname string, shouldExist bool) (
func (a App) updateCover(ctx context.Context, item absto.Item) error {
directory, err := a.storageApp.Info(ctx, item.Dir())
if err != nil {
return fmt.Errorf("get directory: %s", err)
return fmt.Errorf("get directory: %w", err)
}

aggregate, err := a.exifApp.GetAggregateFor(ctx, directory)
if err != nil && !absto.IsNotExist(err) {
return fmt.Errorf("get aggregate: %s", err)
return fmt.Errorf("get aggregate: %w", err)
}

aggregate.Cover = item.Name

if err := a.exifApp.SaveAggregateFor(ctx, directory, aggregate); err != nil {
return fmt.Errorf("save aggregate: %s", err)
return fmt.Errorf("save aggregate: %w", err)
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/crud/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ func parseDate(raw string) (time.Time, error) {

value, err := time.Parse(isoDateLayout, raw)
if err != nil {
return time.Time{}, fmt.Errorf("parse date: %s", err)
return time.Time{}, fmt.Errorf("parse date: %w", err)
}

return value, nil
Expand Down
4 changes: 2 additions & 2 deletions pkg/crud/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (a App) computeStats(ctx context.Context, pathname string) (map[string]uint
return nil
})
if err != nil {
return nil, fmt.Errorf("browse files: %s", err)
return nil, fmt.Errorf("browse files: %w", err)
}

err = a.rawStorageApp.Walk(ctx, provider.MetadataDirectoryName+pathname, func(item absto.Item) error {
Expand All @@ -68,7 +68,7 @@ func (a App) computeStats(ctx context.Context, pathname string) (map[string]uint
})

if err != nil {
return nil, fmt.Errorf("browse metadatas: %s", err)
return nil, fmt.Errorf("browse metadatas: %w", err)
}

return map[string]uint64{
Expand Down
6 changes: 3 additions & 3 deletions pkg/crud/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ func (a App) saveUploadedFile(ctx context.Context, request provider.Request, inp

fileName, filePath, err = getUploadNameAndPath(request, inputName, file)
if err != nil {
return "", fmt.Errorf("get upload name: %s", err)
return "", fmt.Errorf("get upload name: %w", err)
}

var size int64
size, err = getUploadSize(rawSize)
if err != nil {
return "", fmt.Errorf("get upload size: %s", err)
return "", fmt.Errorf("get upload size: %w", err)
}

err = provider.WriteToStorage(ctx, a.storageApp, filePath, size, file)
Expand Down Expand Up @@ -70,7 +70,7 @@ func getUploadSize(rawSize string) (int64, error) {

if len(rawSize) > 0 {
if size, err := strconv.ParseInt(rawSize, 10, 64); err != nil {
return size, fmt.Errorf("parse filesize: %s", err)
return size, fmt.Errorf("parse filesize: %w", err)
}
}

Expand Down
8 changes: 4 additions & 4 deletions pkg/crud/upload_chunk.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func (a App) mergeChunk(w http.ResponseWriter, r *http.Request, request provider
func (a App) mergeChunkFiles(directory, destination string) error {
writer, err := os.OpenFile(destination, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0o600)
if err != nil {
return fmt.Errorf("open destination file `%s`: %s", destination, err)
return fmt.Errorf("open destination file `%s`: %w", destination, err)
}

defer func() {
Expand All @@ -135,7 +135,7 @@ func (a App) mergeChunkFiles(directory, destination string) error {
}()

if err = browseChunkFiles(directory, destination, writer); err != nil {
return fmt.Errorf("walk chunks in `%s`: %s", directory, err)
return fmt.Errorf("walk chunks in `%s`: %w", directory, err)
}

return nil
Expand All @@ -153,7 +153,7 @@ func browseChunkFiles(directory, destination string, writer io.Writer) error {

reader, err := os.Open(path)
if err != nil {
return fmt.Errorf("open chunk `%s`: %s", path, err)
return fmt.Errorf("open chunk `%s`: %w", path, err)
}

defer func() {
Expand All @@ -163,7 +163,7 @@ func browseChunkFiles(directory, destination string, writer io.Writer) error {
}()

if _, err = io.Copy(writer, reader); err != nil {
return fmt.Errorf("copy chunk `%s`: %s", path, err)
return fmt.Errorf("copy chunk `%s`: %w", path, err)
}

return nil
Expand Down
6 changes: 3 additions & 3 deletions pkg/crud/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (a App) createWebhook(w http.ResponseWriter, r *http.Request, request provi
var err error
err = r.ParseForm()
if err != nil {
a.error(w, r, request, model.WrapInvalid(fmt.Errorf("parse form: %s", err)))
a.error(w, r, request, model.WrapInvalid(fmt.Errorf("parse form: %w", err)))
return
}

Expand Down Expand Up @@ -72,7 +72,7 @@ func checkWebhookForm(r *http.Request) (recursive bool, kind provider.WebhookKin

kind, err = provider.ParseWebhookKind(r.Form.Get("kind"))
if err != nil {
err = model.WrapInvalid(fmt.Errorf("parse kind: %s", err))
err = model.WrapInvalid(fmt.Errorf("parse kind: %w", err))
return
}

Expand All @@ -92,7 +92,7 @@ func checkWebhookForm(r *http.Request) (recursive bool, kind provider.WebhookKin
webhookURL = generateTelegramURL(webhookURL, chatID)
} else {
if _, err = url.Parse(webhookURL); err != nil {
err = model.WrapInvalid(fmt.Errorf("parse url: %s", err))
err = model.WrapInvalid(fmt.Errorf("parse url: %w", err))
return
}
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/exif/amqp.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func (a App) AMQPHandler(message amqp.Delivery) error {
var resp provider.ExifResponse

if err := json.Unmarshal(message.Body, &resp); err != nil {
return fmt.Errorf("decode: %s", err)
return fmt.Errorf("decode: %w", err)
}

if resp.Exif.IsZero() {
Expand All @@ -33,7 +33,7 @@ func (a App) AMQPHandler(message amqp.Delivery) error {
resp.Exif.Description = exif.Description

if err := a.SaveExifFor(ctx, resp.Item, resp.Exif); err != nil {
return fmt.Errorf("save: %s", err)
return fmt.Errorf("save: %w", err)
}

return a.processExif(ctx, resp.Item, resp.Exif, true)
Expand Down
2 changes: 1 addition & 1 deletion pkg/exif/date.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func (a App) updateDate(ctx context.Context, item absto.Item, data exas.Exif) er
}

if err := a.storageApp.UpdateDate(ctx, item.Pathname, data.Date); err != nil {
return fmt.Errorf("update date: %s", err)
return fmt.Errorf("update date: %w", err)
}

return nil
Expand Down
20 changes: 10 additions & 10 deletions pkg/exif/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (a App) EventConsumer(ctx context.Context, e provider.Event) {

func (a App) Rename(ctx context.Context, old, new absto.Item) error {
if err := a.storageApp.Rename(ctx, Path(old), Path(new)); err != nil && !absto.IsNotExist(err) {
return fmt.Errorf("rename exif: %s", err)
return fmt.Errorf("rename exif: %w", err)
}

return nil
Expand Down Expand Up @@ -88,7 +88,7 @@ func (a App) handleUploadEvent(ctx context.Context, item absto.Item, aggregate b

exif, err := a.extractAndSaveExif(ctx, item)
if err != nil {
return fmt.Errorf("extract and save exif: %s", err)
return fmt.Errorf("extract and save exif: %w", err)
}

if exif.IsZero() {
Expand All @@ -100,15 +100,15 @@ func (a App) handleUploadEvent(ctx context.Context, item absto.Item, aggregate b

func (a App) processExif(ctx context.Context, item absto.Item, exif exas.Exif, aggregate bool) error {
if err := a.updateDate(ctx, item, exif); err != nil {
return fmt.Errorf("update date: %s", err)
return fmt.Errorf("update date: %w", err)
}

if !aggregate {
return nil
}

if err := a.aggregate(ctx, item); err != nil {
return fmt.Errorf("aggregate folder: %s", err)
return fmt.Errorf("aggregate folder: %w", err)
}

return nil
Expand All @@ -117,37 +117,37 @@ func (a App) processExif(ctx context.Context, item absto.Item, exif exas.Exif, a
func (a App) aggregateOnRename(ctx context.Context, old, new absto.Item) error {
oldDir, err := a.getDirOf(ctx, old)
if err != nil {
return fmt.Errorf("get old directory: %s", err)
return fmt.Errorf("get old directory: %w", err)
}

newDir, err := a.getDirOf(ctx, new)
if err != nil {
return fmt.Errorf("get new directory: %s", err)
return fmt.Errorf("get new directory: %w", err)
}

if oldDir.Pathname == newDir.Pathname {
return nil
}

if err = a.aggregate(ctx, oldDir); err != nil {
return fmt.Errorf("aggregate old directory: %s", err)
return fmt.Errorf("aggregate old directory: %w", err)
}

if err = a.aggregate(ctx, newDir); err != nil {
return fmt.Errorf("aggregate new directory: %s", err)
return fmt.Errorf("aggregate new directory: %w", err)
}

return nil
}

func (a App) delete(ctx context.Context, item absto.Item) error {
if err := a.storageApp.Remove(ctx, Path(item)); err != nil {
return fmt.Errorf("delete: %s", err)
return fmt.Errorf("delete: %w", err)
}

if !item.IsDir {
if err := a.aggregate(ctx, item); err != nil {
return fmt.Errorf("aggregate directory: %s", err)
return fmt.Errorf("aggregate directory: %w", err)
}
}

Expand Down
10 changes: 5 additions & 5 deletions pkg/exif/exif.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func New(config Config, storageApp absto.Storage, prometheusRegisterer prometheu
amqpExchange = strings.TrimSpace(*config.amqpExchange)

if err := amqpClient.Publisher(amqpExchange, "direct", nil); err != nil {
return App{}, fmt.Errorf("configure amqp: %s", err)
return App{}, fmt.Errorf("configure amqp: %w", err)
}
}

Expand Down Expand Up @@ -118,7 +118,7 @@ func (a App) enabled() bool {
func (a App) extractAndSaveExif(ctx context.Context, item absto.Item) (exif exas.Exif, err error) {
exif, err = a.extractExif(ctx, item)
if err != nil {
err = fmt.Errorf("extract exif: %s", err)
err = fmt.Errorf("extract exif: %w", err)
return
}

Expand All @@ -135,7 +135,7 @@ func (a App) extractAndSaveExif(ctx context.Context, item absto.Item) (exif exas
}

if err = a.SaveExifFor(ctx, item, exif); err != nil {
err = fmt.Errorf("save exif: %s", err)
err = fmt.Errorf("save exif: %w", err)
}

return
Expand All @@ -154,12 +154,12 @@ func (a App) extractExif(ctx context.Context, item absto.Item) (exif exas.Exif,

if err != nil {
a.increaseExif("error")
err = fmt.Errorf("fetch exif: %s", err)
err = fmt.Errorf("fetch exif: %w", err)
return
}

if err = httpjson.Read(resp, &exif); err != nil {
err = fmt.Errorf("read exif: %s", err)
err = fmt.Errorf("read exif: %w", err)
}

return
Expand Down
6 changes: 3 additions & 3 deletions pkg/exif/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,16 @@ func (a App) saveMetadata(ctx context.Context, item absto.Item, data any) error

if _, err := a.storageApp.Info(ctx, dirname); err != nil {
if !absto.IsNotExist(err) {
return fmt.Errorf("check directory existence: %s", err)
return fmt.Errorf("check directory existence: %w", err)
}

if err = a.storageApp.CreateDir(ctx, dirname); err != nil {
return fmt.Errorf("create directory: %s", err)
return fmt.Errorf("create directory: %w", err)
}
}

if err := provider.SaveJSON(ctx, a.storageApp, filename, data); err != nil {
return fmt.Errorf("save: %s", err)
return fmt.Errorf("save: %w", err)
}

if item.IsDir {
Expand Down
Loading

0 comments on commit f5c2682

Please sign in to comment.