Clarify existing repository errors
This commit is contained in:
@@ -239,18 +239,32 @@ func (r *Runner) run(ctx context.Context, repo model.Repository, args []string,
|
||||
*capture = captured
|
||||
}
|
||||
if err != nil {
|
||||
message := redact(string(errBytes), repo)
|
||||
message := strings.TrimSpace(redact(string(errBytes), repo))
|
||||
if errors.Is(ctx.Err(), context.Canceled) {
|
||||
return fmt.Errorf("cancelled: %w", ctx.Err())
|
||||
}
|
||||
if strings.Contains(message, "repository does not exist") || strings.Contains(message, "Is there a repository at the following location?") {
|
||||
return errors.New("repository: destination is not initialized; open Repositories and select Initialize")
|
||||
}
|
||||
return fmt.Errorf("restic: %s: %w", strings.TrimSpace(message), err)
|
||||
return resticError(message, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func resticError(message string, commandErr error) error {
|
||||
lower := strings.ToLower(message)
|
||||
switch {
|
||||
case strings.Contains(lower, "config file already exists") || strings.Contains(lower, "repository master key and config already initialized"):
|
||||
return errors.New("repository: destination is already initialized; do not select Initialize again. Enter the existing repository password and select Test")
|
||||
case strings.Contains(lower, "ciphertext verification failed"):
|
||||
return errors.New("authentication: the saved repository password does not match this existing repository, or its Restic key is damaged. Enter the original repository password under Edit and select Test")
|
||||
case strings.Contains(lower, "wrong password") || strings.Contains(lower, "no key found"):
|
||||
return errors.New("authentication: wrong repository password. Enter the original Restic password under Edit and select Test")
|
||||
case strings.Contains(lower, "repository does not exist") || strings.Contains(lower, "is there a repository at the following location?"):
|
||||
return errors.New("repository: destination is not initialized; open Repositories and select Initialize once")
|
||||
default:
|
||||
message = strings.TrimSpace(strings.TrimPrefix(message, "Fatal:"))
|
||||
return fmt.Errorf("restic: %s: %w", message, commandErr)
|
||||
}
|
||||
}
|
||||
|
||||
func repositoryLocation(repo model.Repository) string {
|
||||
if repo.Type == model.RepositorySFTP && !strings.HasPrefix(repo.Location, "sftp:") {
|
||||
return "sftp:" + repo.Location
|
||||
|
||||
@@ -74,3 +74,17 @@ func TestForgetUsesAgeAndCalendarRetention(t *testing.T) {
|
||||
t.Fatalf("disabled daily retention was included: %s", args)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResticErrorExplainsExistingRepository(t *testing.T) {
|
||||
err := resticError("Fatal: create repository failed: config file already exists", fmt.Errorf("exit status 1"))
|
||||
if !strings.Contains(err.Error(), "already initialized") || !strings.Contains(err.Error(), "select Test") {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResticErrorExplainsCiphertextVerification(t *testing.T) {
|
||||
err := resticError(`{"message_type":"exit_error","message":"Fatal: config or key abc is damaged: ciphertext verification failed"}`, fmt.Errorf("exit status 1"))
|
||||
if !strings.Contains(err.Error(), "password does not match") || strings.Contains(err.Error(), "abc") {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user