mirror of
				https://github.com/usememos/memos.git
				synced 2025-06-05 22:09:59 +02:00 
			
		
		
		
	chore: update error format
This commit is contained in:
		| @@ -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 { | 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()}) | 	systemSettingStorageServiceID, err := s.GetSystemSetting(ctx, &store.FindSystemSetting{Name: SystemSettingStorageServiceIDName.String()}) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return errors.Errorf("Failed to find SystemSettingStorageServiceIDName: %s", err) | 		return errors.Wrap(err, "Failed to find SystemSettingStorageServiceIDName") | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	storageServiceID := LocalStorage | 	storageServiceID := LocalStorage | ||||||
| 	if systemSettingStorageServiceID != nil { | 	if systemSettingStorageServiceID != nil { | ||||||
| 		err = json.Unmarshal([]byte(systemSettingStorageServiceID.Value), &storageServiceID) | 		err = json.Unmarshal([]byte(systemSettingStorageServiceID.Value), &storageServiceID) | ||||||
| 		if err != nil { | 		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 { | 	if storageServiceID == DatabaseStorage { | ||||||
| 		fileBytes, err := io.ReadAll(r) | 		fileBytes, err := io.ReadAll(r) | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
| 			return errors.Errorf("Failed to read file: %s", err) | 			return errors.Wrap(err, "Failed to read file") | ||||||
| 		} | 		} | ||||||
| 		create.Blob = fileBytes | 		create.Blob = fileBytes | ||||||
| 		return nil | 		return nil | ||||||
| @@ -588,13 +588,13 @@ func SaveResourceBlob(ctx context.Context, s *store.Store, create *store.Resourc | |||||||
| 		// `LocalStorage` means save blob into local disk | 		// `LocalStorage` means save blob into local disk | ||||||
| 		systemSettingLocalStoragePath, err := s.GetSystemSetting(ctx, &store.FindSystemSetting{Name: SystemSettingLocalStoragePathName.String()}) | 		systemSettingLocalStoragePath, err := s.GetSystemSetting(ctx, &store.FindSystemSetting{Name: SystemSettingLocalStoragePathName.String()}) | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
| 			return errors.Errorf("Failed to find SystemSettingLocalStoragePathName: %s", err) | 			return errors.Wrap(err, "Failed to find SystemSettingLocalStoragePathName") | ||||||
| 		} | 		} | ||||||
| 		localStoragePath := "assets/{timestamp}_{filename}" | 		localStoragePath := "assets/{timestamp}_{filename}" | ||||||
| 		if systemSettingLocalStoragePath != nil && systemSettingLocalStoragePath.Value != "" { | 		if systemSettingLocalStoragePath != nil && systemSettingLocalStoragePath.Value != "" { | ||||||
| 			err = json.Unmarshal([]byte(systemSettingLocalStoragePath.Value), &localStoragePath) | 			err = json.Unmarshal([]byte(systemSettingLocalStoragePath.Value), &localStoragePath) | ||||||
| 			if err != nil { | 			if err != nil { | ||||||
| 				return errors.Errorf("Failed to unmarshal SystemSettingLocalStoragePathName: %s", err) | 				return errors.Wrap(err, "Failed to unmarshal SystemSettingLocalStoragePathName") | ||||||
| 			} | 			} | ||||||
| 		} | 		} | ||||||
| 		filePath := filepath.FromSlash(localStoragePath) | 		filePath := filepath.FromSlash(localStoragePath) | ||||||
| @@ -605,16 +605,16 @@ func SaveResourceBlob(ctx context.Context, s *store.Store, create *store.Resourc | |||||||
|  |  | ||||||
| 		dir := filepath.Dir(filePath) | 		dir := filepath.Dir(filePath) | ||||||
| 		if err = os.MkdirAll(dir, os.ModePerm); err != nil { | 		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) | 		dst, err := os.Create(filePath) | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
| 			return errors.Errorf("Failed to create file: %s", err) | 			return errors.Wrap(err, "Failed to create file") | ||||||
| 		} | 		} | ||||||
| 		defer dst.Close() | 		defer dst.Close() | ||||||
| 		_, err = io.Copy(dst, r) | 		_, err = io.Copy(dst, r) | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
| 			return errors.Errorf("Failed to copy file: %s", err) | 			return errors.Wrap(err, "Failed to copy file") | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
| 		create.InternalPath = filePath | 		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 | 	// Others: store blob into external service, such as S3 | ||||||
| 	storage, err := s.GetStorage(ctx, &store.FindStorage{ID: &storageServiceID}) | 	storage, err := s.GetStorage(ctx, &store.FindStorage{ID: &storageServiceID}) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return errors.Errorf("Failed to find StorageServiceID: %s", err) | 		return errors.Wrap(err, "Failed to find StorageServiceID") | ||||||
| 	} | 	} | ||||||
| 	if storage == nil { | 	if storage == nil { | ||||||
| 		return errors.Errorf("Storage %d not found", storageServiceID) | 		return errors.Errorf("Storage %d not found", storageServiceID) | ||||||
| 	} | 	} | ||||||
| 	storageMessage, err := ConvertStorageFromStore(storage) | 	storageMessage, err := ConvertStorageFromStore(storage) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return errors.Errorf("Failed to ConvertStorageFromStore: %s", err) | 		return errors.Wrap(err, "Failed to ConvertStorageFromStore") | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	if storageMessage.Type != StorageS3 { | 	if storageMessage.Type != StorageS3 { | ||||||
| @@ -649,7 +649,7 @@ func SaveResourceBlob(ctx context.Context, s *store.Store, create *store.Resourc | |||||||
| 		URLSuffix: s3Config.URLSuffix, | 		URLSuffix: s3Config.URLSuffix, | ||||||
| 	}) | 	}) | ||||||
| 	if err != nil { | 	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 | 	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) | 	link, err := s3Client.UploadFile(ctx, filePath, create.Type, r) | ||||||
| 	if err != nil { | 	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 | 	create.ExternalLink = link | ||||||
|   | |||||||
| @@ -248,7 +248,7 @@ func (upsert UpsertSystemSettingRequest) Validate() error { | |||||||
| 			return errors.Errorf(systemSettingUnmarshalError, settingName) | 			return errors.Errorf(systemSettingUnmarshalError, settingName) | ||||||
| 		} | 		} | ||||||
| 		if value < 0 { | 		if value < 0 { | ||||||
| 			return errors.Errorf("must be positive") | 			return errors.New("must be positive") | ||||||
| 		} | 		} | ||||||
| 	case SystemSettingTelegramBotTokenName: | 	case SystemSettingTelegramBotTokenName: | ||||||
| 		if upsert.Value == "" { | 		if upsert.Value == "" { | ||||||
| @@ -260,7 +260,7 @@ func (upsert UpsertSystemSettingRequest) Validate() error { | |||||||
| 			if strings.HasPrefix(upsert.Value[slashIndex:], "/bot") { | 			if strings.HasPrefix(upsert.Value[slashIndex:], "/bot") { | ||||||
| 				return nil | 				return nil | ||||||
| 			} | 			} | ||||||
| 			return errors.Errorf("token start with `http` must end with `/bot<token>`") | 			return errors.New("token start with `http` must end with `/bot<token>`") | ||||||
| 		} | 		} | ||||||
| 		fragments := strings.Split(upsert.Value, ":") | 		fragments := strings.Split(upsert.Value, ":") | ||||||
| 		if len(fragments) != 2 { | 		if len(fragments) != 2 { | ||||||
| @@ -272,7 +272,7 @@ func (upsert UpsertSystemSettingRequest) Validate() error { | |||||||
| 			return errors.Errorf(systemSettingUnmarshalError, settingName) | 			return errors.Errorf(systemSettingUnmarshalError, settingName) | ||||||
| 		} | 		} | ||||||
| 	default: | 	default: | ||||||
| 		return errors.Errorf("invalid system setting name") | 		return errors.New("invalid system setting name") | ||||||
| 	} | 	} | ||||||
| 	return nil | 	return nil | ||||||
| } | } | ||||||
|   | |||||||
| @@ -412,26 +412,26 @@ func (s *APIV1Service) UpdateUser(c echo.Context) error { | |||||||
|  |  | ||||||
| func (create CreateUserRequest) Validate() error { | func (create CreateUserRequest) Validate() error { | ||||||
| 	if len(create.Username) < 3 { | 	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 { | 	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 { | 	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 { | 	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 { | 	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 create.Email != "" { | ||||||
| 		if len(create.Email) > 256 { | 		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) { | 		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 { | func (update UpdateUserRequest) Validate() error { | ||||||
| 	if update.Username != nil && len(*update.Username) < 3 { | 	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 { | 	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 { | 	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 { | 	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 { | 	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 update.AvatarURL != nil { | ||||||
| 		if len(*update.AvatarURL) > 2<<20 { | 		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 update.Email != nil && *update.Email != "" { | ||||||
| 		if len(*update.Email) > 256 { | 		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) { | 		if !util.ValidateEmail(*update.Email) { | ||||||
| 			return errors.Errorf("invalid email format") | 			return errors.New("invalid email format") | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|   | |||||||
| @@ -128,37 +128,37 @@ func (upsert UpsertUserSettingRequest) Validate() error { | |||||||
| 		localeValue := "en" | 		localeValue := "en" | ||||||
| 		err := json.Unmarshal([]byte(upsert.Value), &localeValue) | 		err := json.Unmarshal([]byte(upsert.Value), &localeValue) | ||||||
| 		if err != nil { | 		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) { | 		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 { | 	} else if upsert.Key == UserSettingAppearanceKey { | ||||||
| 		appearanceValue := "system" | 		appearanceValue := "system" | ||||||
| 		err := json.Unmarshal([]byte(upsert.Value), &appearanceValue) | 		err := json.Unmarshal([]byte(upsert.Value), &appearanceValue) | ||||||
| 		if err != nil { | 		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) { | 		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 { | 	} else if upsert.Key == UserSettingMemoVisibilityKey { | ||||||
| 		memoVisibilityValue := Private | 		memoVisibilityValue := Private | ||||||
| 		err := json.Unmarshal([]byte(upsert.Value), &memoVisibilityValue) | 		err := json.Unmarshal([]byte(upsert.Value), &memoVisibilityValue) | ||||||
| 		if err != nil { | 		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) { | 		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 { | 	} else if upsert.Key == UserSettingTelegramUserIDKey { | ||||||
| 		var key string | 		var key string | ||||||
| 		err := json.Unmarshal([]byte(upsert.Value), &key) | 		err := json.Unmarshal([]byte(upsert.Value), &key) | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
| 			return errors.Errorf("invalid user setting telegram user id value") | 			return errors.New("invalid user setting telegram user id value") | ||||||
| 		} | 		} | ||||||
| 	} else { | 	} else { | ||||||
| 		return errors.Errorf("invalid user setting key") | 		return errors.New("invalid user setting key") | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	return nil | 	return nil | ||||||
|   | |||||||
| @@ -132,7 +132,7 @@ func getTokenFromMetadata(md metadata.MD) (string, error) { | |||||||
| 	if len(md.Get("Authorization")) > 0 { | 	if len(md.Get("Authorization")) > 0 { | ||||||
| 		authHeaderParts := strings.Fields(authorizationHeaders[0]) | 		authHeaderParts := strings.Fields(authorizationHeaders[0]) | ||||||
| 		if len(authHeaderParts) != 2 || strings.ToLower(authHeaderParts[0]) != "bearer" { | 		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 | 		return authHeaderParts[1], nil | ||||||
| 	} | 	} | ||||||
|   | |||||||
| @@ -170,7 +170,7 @@ func (d *Driver) ListMemos(ctx context.Context, find *store.FindMemo) ([]*store. | |||||||
| 			for _, relatedMemoType := range relatedMemoTypeList { | 			for _, relatedMemoType := range relatedMemoTypeList { | ||||||
| 				relatedMemoTypeList := strings.Split(relatedMemoType, ":") | 				relatedMemoTypeList := strings.Split(relatedMemoType, ":") | ||||||
| 				if len(relatedMemoTypeList) != 2 { | 				if len(relatedMemoTypeList) != 2 { | ||||||
| 					return nil, errors.Errorf("invalid relation format") | 					return nil, errors.New("invalid relation format") | ||||||
| 				} | 				} | ||||||
| 				relatedMemoID, err := util.ConvertStringToInt32(relatedMemoTypeList[0]) | 				relatedMemoID, err := util.ConvertStringToInt32(relatedMemoTypeList[0]) | ||||||
| 				if err != nil { | 				if err != nil { | ||||||
|   | |||||||
| @@ -103,7 +103,7 @@ func vacuumImpl(ctx context.Context, tx *sql.Tx) error { | |||||||
| func (d *Driver) BackupTo(ctx context.Context, filename string) error { | func (d *Driver) BackupTo(ctx context.Context, filename string) error { | ||||||
| 	conn, err := d.db.Conn(ctx) | 	conn, err := d.db.Conn(ctx) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return errors.Errorf("fail to get conn %s", err) | 		return errors.Wrap(err, "fail to open new connection") | ||||||
| 	} | 	} | ||||||
| 	defer conn.Close() | 	defer conn.Close() | ||||||
|  |  | ||||||
| @@ -113,25 +113,25 @@ func (d *Driver) BackupTo(ctx context.Context, filename string) error { | |||||||
| 		} | 		} | ||||||
| 		backupConn, ok := driverConn.(backuper) | 		backupConn, ok := driverConn.(backuper) | ||||||
| 		if !ok { | 		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) | 		bck, err := backupConn.NewBackup(filename) | ||||||
| 		if err != nil { | 		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; { | 		for more := true; more; { | ||||||
| 			more, err = bck.Step(-1) | 			more, err = bck.Step(-1) | ||||||
| 			if err != nil { | 			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() | 		return bck.Finish() | ||||||
| 	}) | 	}) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return errors.Errorf("fail to backup %s", err) | 		return errors.Wrap(err, "fail to backup") | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	return nil | 	return nil | ||||||
|   | |||||||
| @@ -142,7 +142,7 @@ func (s *TestingServer) request(method, uri string, body io.Reader, params, head | |||||||
| 				} | 				} | ||||||
| 			} | 			} | ||||||
| 			if cookie == "" { | 			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 | 			s.cookie = cookie | ||||||
| 		} else if strings.Contains(uri, "/api/v1/auth/signout") { | 		} else if strings.Contains(uri, "/api/v1/auth/signout") { | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user