From a928c4f845891fbef9c3eeefb4860ff923bb3488 Mon Sep 17 00:00:00 2001 From: steven Date: Fri, 29 Sep 2023 13:04:54 +0800 Subject: [PATCH] chore: update error format --- api/v1/resource.go | 24 ++++++++++++------------ api/v1/system_setting.go | 6 +++--- api/v1/user.go | 30 +++++++++++++++--------------- api/v1/user_setting.go | 16 ++++++++-------- api/v2/acl.go | 2 +- store/sqlite/memo.go | 2 +- store/sqlite/sqlite.go | 10 +++++----- test/server/server.go | 2 +- 8 files changed, 46 insertions(+), 46 deletions(-) diff --git a/api/v1/resource.go b/api/v1/resource.go index a297cfa6..f8ae3d17 100644 --- a/api/v1/resource.go +++ b/api/v1/resource.go @@ -565,14 +565,14 @@ func convertResourceFromStore(resource *store.Resource) *Resource { func SaveResourceBlob(ctx context.Context, s *store.Store, create *store.Resource, r io.Reader) error { systemSettingStorageServiceID, err := s.GetSystemSetting(ctx, &store.FindSystemSetting{Name: SystemSettingStorageServiceIDName.String()}) if err != nil { - return errors.Errorf("Failed to find SystemSettingStorageServiceIDName: %s", err) + return errors.Wrap(err, "Failed to find SystemSettingStorageServiceIDName") } storageServiceID := LocalStorage if systemSettingStorageServiceID != nil { err = json.Unmarshal([]byte(systemSettingStorageServiceID.Value), &storageServiceID) if err != nil { - return errors.Errorf("Failed to unmarshal storage service id: %s", err) + return errors.Wrap(err, "Failed to unmarshal storage service id") } } @@ -580,7 +580,7 @@ func SaveResourceBlob(ctx context.Context, s *store.Store, create *store.Resourc if storageServiceID == DatabaseStorage { fileBytes, err := io.ReadAll(r) if err != nil { - return errors.Errorf("Failed to read file: %s", err) + return errors.Wrap(err, "Failed to read file") } create.Blob = fileBytes return nil @@ -588,13 +588,13 @@ func SaveResourceBlob(ctx context.Context, s *store.Store, create *store.Resourc // `LocalStorage` means save blob into local disk systemSettingLocalStoragePath, err := s.GetSystemSetting(ctx, &store.FindSystemSetting{Name: SystemSettingLocalStoragePathName.String()}) if err != nil { - return errors.Errorf("Failed to find SystemSettingLocalStoragePathName: %s", err) + return errors.Wrap(err, "Failed to find SystemSettingLocalStoragePathName") } localStoragePath := "assets/{timestamp}_{filename}" if systemSettingLocalStoragePath != nil && systemSettingLocalStoragePath.Value != "" { err = json.Unmarshal([]byte(systemSettingLocalStoragePath.Value), &localStoragePath) if err != nil { - return errors.Errorf("Failed to unmarshal SystemSettingLocalStoragePathName: %s", err) + return errors.Wrap(err, "Failed to unmarshal SystemSettingLocalStoragePathName") } } filePath := filepath.FromSlash(localStoragePath) @@ -605,16 +605,16 @@ func SaveResourceBlob(ctx context.Context, s *store.Store, create *store.Resourc dir := filepath.Dir(filePath) if err = os.MkdirAll(dir, os.ModePerm); err != nil { - return errors.Errorf("Failed to create directory: %s", err) + return errors.Wrap(err, "Failed to create directory") } dst, err := os.Create(filePath) if err != nil { - return errors.Errorf("Failed to create file: %s", err) + return errors.Wrap(err, "Failed to create file") } defer dst.Close() _, err = io.Copy(dst, r) if err != nil { - return errors.Errorf("Failed to copy file: %s", err) + return errors.Wrap(err, "Failed to copy file") } create.InternalPath = filePath @@ -624,14 +624,14 @@ func SaveResourceBlob(ctx context.Context, s *store.Store, create *store.Resourc // Others: store blob into external service, such as S3 storage, err := s.GetStorage(ctx, &store.FindStorage{ID: &storageServiceID}) if err != nil { - return errors.Errorf("Failed to find StorageServiceID: %s", err) + return errors.Wrap(err, "Failed to find StorageServiceID") } if storage == nil { return errors.Errorf("Storage %d not found", storageServiceID) } storageMessage, err := ConvertStorageFromStore(storage) if err != nil { - return errors.Errorf("Failed to ConvertStorageFromStore: %s", err) + return errors.Wrap(err, "Failed to ConvertStorageFromStore") } if storageMessage.Type != StorageS3 { @@ -649,7 +649,7 @@ func SaveResourceBlob(ctx context.Context, s *store.Store, create *store.Resourc URLSuffix: s3Config.URLSuffix, }) if err != nil { - return errors.Errorf("Failed to create s3 client: %s", err) + return errors.Wrap(err, "Failed to create s3 client") } filePath := s3Config.Path @@ -660,7 +660,7 @@ func SaveResourceBlob(ctx context.Context, s *store.Store, create *store.Resourc link, err := s3Client.UploadFile(ctx, filePath, create.Type, r) if err != nil { - return errors.Errorf("Failed to upload via s3 client: %s", err) + return errors.Wrap(err, "Failed to upload via s3 client") } create.ExternalLink = link diff --git a/api/v1/system_setting.go b/api/v1/system_setting.go index f7151322..708e812b 100644 --- a/api/v1/system_setting.go +++ b/api/v1/system_setting.go @@ -248,7 +248,7 @@ func (upsert UpsertSystemSettingRequest) Validate() error { return errors.Errorf(systemSettingUnmarshalError, settingName) } if value < 0 { - return errors.Errorf("must be positive") + return errors.New("must be positive") } case SystemSettingTelegramBotTokenName: if upsert.Value == "" { @@ -260,7 +260,7 @@ func (upsert UpsertSystemSettingRequest) Validate() error { if strings.HasPrefix(upsert.Value[slashIndex:], "/bot") { return nil } - return errors.Errorf("token start with `http` must end with `/bot`") + return errors.New("token start with `http` must end with `/bot`") } fragments := strings.Split(upsert.Value, ":") if len(fragments) != 2 { @@ -272,7 +272,7 @@ func (upsert UpsertSystemSettingRequest) Validate() error { return errors.Errorf(systemSettingUnmarshalError, settingName) } default: - return errors.Errorf("invalid system setting name") + return errors.New("invalid system setting name") } return nil } diff --git a/api/v1/user.go b/api/v1/user.go index fca131ad..0b20c7ca 100644 --- a/api/v1/user.go +++ b/api/v1/user.go @@ -412,26 +412,26 @@ func (s *APIV1Service) UpdateUser(c echo.Context) error { func (create CreateUserRequest) Validate() error { if len(create.Username) < 3 { - return errors.Errorf("username is too short, minimum length is 3") + return errors.New("username is too short, minimum length is 3") } if len(create.Username) > 32 { - return errors.Errorf("username is too long, maximum length is 32") + return errors.New("username is too long, maximum length is 32") } if len(create.Password) < 3 { - return errors.Errorf("password is too short, minimum length is 3") + return errors.New("password is too short, minimum length is 3") } if len(create.Password) > 512 { - return errors.Errorf("password is too long, maximum length is 512") + return errors.New("password is too long, maximum length is 512") } if len(create.Nickname) > 64 { - return errors.Errorf("nickname is too long, maximum length is 64") + return errors.New("nickname is too long, maximum length is 64") } if create.Email != "" { if len(create.Email) > 256 { - return errors.Errorf("email is too long, maximum length is 256") + return errors.New("email is too long, maximum length is 256") } if !util.ValidateEmail(create.Email) { - return errors.Errorf("invalid email format") + return errors.New("invalid email format") } } @@ -440,31 +440,31 @@ func (create CreateUserRequest) Validate() error { func (update UpdateUserRequest) Validate() error { if update.Username != nil && len(*update.Username) < 3 { - return errors.Errorf("username is too short, minimum length is 3") + return errors.New("username is too short, minimum length is 3") } if update.Username != nil && len(*update.Username) > 32 { - return errors.Errorf("username is too long, maximum length is 32") + return errors.New("username is too long, maximum length is 32") } if update.Password != nil && len(*update.Password) < 3 { - return errors.Errorf("password is too short, minimum length is 3") + return errors.New("password is too short, minimum length is 3") } if update.Password != nil && len(*update.Password) > 512 { - return errors.Errorf("password is too long, maximum length is 512") + return errors.New("password is too long, maximum length is 512") } if update.Nickname != nil && len(*update.Nickname) > 64 { - return errors.Errorf("nickname is too long, maximum length is 64") + return errors.New("nickname is too long, maximum length is 64") } if update.AvatarURL != nil { if len(*update.AvatarURL) > 2<<20 { - return errors.Errorf("avatar is too large, maximum is 2MB") + return errors.New("avatar is too large, maximum is 2MB") } } if update.Email != nil && *update.Email != "" { if len(*update.Email) > 256 { - return errors.Errorf("email is too long, maximum length is 256") + return errors.New("email is too long, maximum length is 256") } if !util.ValidateEmail(*update.Email) { - return errors.Errorf("invalid email format") + return errors.New("invalid email format") } } diff --git a/api/v1/user_setting.go b/api/v1/user_setting.go index b808fc32..63bf69d9 100644 --- a/api/v1/user_setting.go +++ b/api/v1/user_setting.go @@ -128,37 +128,37 @@ func (upsert UpsertUserSettingRequest) Validate() error { localeValue := "en" err := json.Unmarshal([]byte(upsert.Value), &localeValue) if err != nil { - return errors.Errorf("failed to unmarshal user setting locale value") + return errors.New("failed to unmarshal user setting locale value") } if !slices.Contains(UserSettingLocaleValue, localeValue) { - return errors.Errorf("invalid user setting locale value") + return errors.New("invalid user setting locale value") } } else if upsert.Key == UserSettingAppearanceKey { appearanceValue := "system" err := json.Unmarshal([]byte(upsert.Value), &appearanceValue) if err != nil { - return errors.Errorf("failed to unmarshal user setting appearance value") + return errors.New("failed to unmarshal user setting appearance value") } if !slices.Contains(UserSettingAppearanceValue, appearanceValue) { - return errors.Errorf("invalid user setting appearance value") + return errors.New("invalid user setting appearance value") } } else if upsert.Key == UserSettingMemoVisibilityKey { memoVisibilityValue := Private err := json.Unmarshal([]byte(upsert.Value), &memoVisibilityValue) if err != nil { - return errors.Errorf("failed to unmarshal user setting memo visibility value") + return errors.New("failed to unmarshal user setting memo visibility value") } if !slices.Contains(UserSettingMemoVisibilityValue, memoVisibilityValue) { - return errors.Errorf("invalid user setting memo visibility value") + return errors.New("invalid user setting memo visibility value") } } else if upsert.Key == UserSettingTelegramUserIDKey { var key string err := json.Unmarshal([]byte(upsert.Value), &key) if err != nil { - return errors.Errorf("invalid user setting telegram user id value") + return errors.New("invalid user setting telegram user id value") } } else { - return errors.Errorf("invalid user setting key") + return errors.New("invalid user setting key") } return nil diff --git a/api/v2/acl.go b/api/v2/acl.go index 1c44b3ec..854af433 100644 --- a/api/v2/acl.go +++ b/api/v2/acl.go @@ -132,7 +132,7 @@ func getTokenFromMetadata(md metadata.MD) (string, error) { if len(md.Get("Authorization")) > 0 { authHeaderParts := strings.Fields(authorizationHeaders[0]) if len(authHeaderParts) != 2 || strings.ToLower(authHeaderParts[0]) != "bearer" { - return "", errors.Errorf("authorization header format must be Bearer {token}") + return "", errors.New("authorization header format must be Bearer {token}") } return authHeaderParts[1], nil } diff --git a/store/sqlite/memo.go b/store/sqlite/memo.go index 9459c3e1..4d66dc10 100644 --- a/store/sqlite/memo.go +++ b/store/sqlite/memo.go @@ -170,7 +170,7 @@ func (d *Driver) ListMemos(ctx context.Context, find *store.FindMemo) ([]*store. for _, relatedMemoType := range relatedMemoTypeList { relatedMemoTypeList := strings.Split(relatedMemoType, ":") if len(relatedMemoTypeList) != 2 { - return nil, errors.Errorf("invalid relation format") + return nil, errors.New("invalid relation format") } relatedMemoID, err := util.ConvertStringToInt32(relatedMemoTypeList[0]) if err != nil { diff --git a/store/sqlite/sqlite.go b/store/sqlite/sqlite.go index 279eb14c..3cd1bb1f 100644 --- a/store/sqlite/sqlite.go +++ b/store/sqlite/sqlite.go @@ -103,7 +103,7 @@ func vacuumImpl(ctx context.Context, tx *sql.Tx) error { func (d *Driver) BackupTo(ctx context.Context, filename string) error { conn, err := d.db.Conn(ctx) if err != nil { - return errors.Errorf("fail to get conn %s", err) + return errors.Wrap(err, "fail to open new connection") } defer conn.Close() @@ -113,25 +113,25 @@ func (d *Driver) BackupTo(ctx context.Context, filename string) error { } backupConn, ok := driverConn.(backuper) if !ok { - return errors.Errorf("db connection is not a sqlite backuper") + return errors.New("db connection is not a sqlite backuper") } bck, err := backupConn.NewBackup(filename) if err != nil { - return errors.Errorf("fail to create sqlite backup %s", err) + return errors.Wrap(err, "fail to create sqlite backup") } for more := true; more; { more, err = bck.Step(-1) if err != nil { - return errors.Errorf("fail to execute sqlite backup %s", err) + return errors.Wrap(err, "fail to execute sqlite backup") } } return bck.Finish() }) if err != nil { - return errors.Errorf("fail to backup %s", err) + return errors.Wrap(err, "fail to backup") } return nil diff --git a/test/server/server.go b/test/server/server.go index 6359eba2..ee262a9d 100644 --- a/test/server/server.go +++ b/test/server/server.go @@ -142,7 +142,7 @@ func (s *TestingServer) request(method, uri string, body io.Reader, params, head } } if cookie == "" { - return nil, errors.Errorf("unable to find access token in the login response headers") + return nil, errors.New("unable to find access token in the login response headers") } s.cookie = cookie } else if strings.Contains(uri, "/api/v1/auth/signout") {