From 23b90e288c2dcd8ce39cc318b36c25f5acb8af75 Mon Sep 17 00:00:00 2001 From: wdvxdr Date: Mon, 25 Jan 2021 17:34:23 +0800 Subject: [PATCH] better error --- global/codec.go | 10 +++++----- global/codec/codec.go | 8 ++++---- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/global/codec.go b/global/codec.go index 188504f..1a6b35f 100644 --- a/global/codec.go +++ b/global/codec.go @@ -2,13 +2,13 @@ package global import ( "crypto/md5" - "errors" "fmt" "io/ioutil" "os/exec" "path" "github.com/Mrs4s/go-cqhttp/global/codec" + "github.com/pkg/errors" log "github.com/sirupsen/logrus" ) @@ -32,7 +32,7 @@ func EncoderSilk(data []byte) ([]byte, error) { h := md5.New() _, err := h.Write(data) if err != nil { - return nil, err + return nil, errors.Wrap(err, "calc md5 failed") } tempName := fmt.Sprintf("%x", h.Sum(nil)) if silkPath := path.Join("data/cache", tempName+".silk"); PathExists(silkPath) { @@ -40,7 +40,7 @@ func EncoderSilk(data []byte) ([]byte, error) { } slk, err := codec.EncodeToSilk(data, tempName, true) if err != nil { - return nil, err + return nil, errors.Wrap(err, "encode silk failed") } return slk, nil } @@ -51,7 +51,7 @@ func EncodeMP4(src string, dst string) error { // -y 覆盖文件 err := cmd1.Run() if err != nil { cmd2 := exec.Command("ffmpeg", "-i", src, "-y", "-c:v", "h264", "-c:a", "mp3", dst) - return cmd2.Run() + return errors.Wrap(cmd2.Run(), "convert mp4 failed") } return err } @@ -59,5 +59,5 @@ func EncodeMP4(src string, dst string) error { // -y 覆盖文件 //ExtractCover 获取给定视频文件的Cover func ExtractCover(src string, target string) error { cmd := exec.Command("ffmpeg", "-i", src, "-y", "-r", "1", "-f", "image2", target) - return cmd.Run() + return errors.Wrap(cmd.Run(), "extract video cover failed") } diff --git a/global/codec/codec.go b/global/codec/codec.go index 6b053db..9b6115c 100644 --- a/global/codec/codec.go +++ b/global/codec/codec.go @@ -4,7 +4,7 @@ package codec import ( - "errors" + "github.com/pkg/errors" "io/ioutil" "net/http" "os" @@ -63,7 +63,7 @@ func EncodeToSilk(record []byte, tempName string, useCache bool) ([]byte, error) rawPath := path.Join(silkCachePath, tempName+".wav") err := ioutil.WriteFile(rawPath, record, os.ModePerm) if err != nil { - return nil, err + return nil, errors.Wrap(err, "write temp file error") } defer os.Remove(rawPath) @@ -71,7 +71,7 @@ func EncodeToSilk(record []byte, tempName string, useCache bool) ([]byte, error) pcmPath := path.Join(silkCachePath, tempName+".pcm") cmd := exec.Command("ffmpeg", "-i", rawPath, "-f", "s16le", "-ar", "24000", "-ac", "1", pcmPath) if err = cmd.Run(); err != nil { - return nil, err + return nil, errors.Wrap(err, "convert pcm file error") } defer os.Remove(pcmPath) @@ -79,7 +79,7 @@ func EncodeToSilk(record []byte, tempName string, useCache bool) ([]byte, error) silkPath := path.Join(silkCachePath, tempName+".silk") cmd = exec.Command(getEncoderFilePath(), pcmPath, silkPath, "-rate", "24000", "-quiet", "-tencent") if err = cmd.Run(); err != nil { - return nil, err + return nil, errors.Wrap(err, "convert silk file error") } if !useCache { defer os.Remove(silkPath)