Add any type and output errors
This commit is contained in:
@@ -2,10 +2,9 @@ package account
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"password/output"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/fatih/color"
|
||||
)
|
||||
|
||||
type ByteReader interface {
|
||||
@@ -46,7 +45,7 @@ func NewVault(db Db) *VaultWithDb {
|
||||
var vault Vault
|
||||
err = json.Unmarshal(file, &vault)
|
||||
if err != nil {
|
||||
color.Red("Не удалось разобрать файл data.json")
|
||||
output.PrintError("Не удалось разобрать файл data.json")
|
||||
return &VaultWithDb{
|
||||
Vault: Vault{
|
||||
Accounts: []Account{},
|
||||
@@ -105,7 +104,7 @@ func (vault *VaultWithDb) save() {
|
||||
vault.UpdatedAt = time.Now()
|
||||
data, err := vault.Vault.ToBytes()
|
||||
if err != nil {
|
||||
color.Red("Не удалось преобразовать")
|
||||
output.PrintError("Не удалось преобразовать")
|
||||
}
|
||||
vault.db.Write(data)
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package files
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"password/output"
|
||||
)
|
||||
|
||||
type JsonDb struct {
|
||||
@@ -26,13 +27,13 @@ func (db *JsonDb) Read() ([]byte, error) {
|
||||
func (db *JsonDb) Write(content []byte) {
|
||||
file, err := os.Create(db.filename)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
output.PrintError(err)
|
||||
}
|
||||
defer file.Close()
|
||||
_, err = file.Write(content)
|
||||
if err != nil {
|
||||
file.Close()
|
||||
fmt.Println(err)
|
||||
output.PrintError(err)
|
||||
return
|
||||
}
|
||||
fmt.Println("Файл успешно записан")
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"fmt"
|
||||
"password/account"
|
||||
"password/files"
|
||||
"password/output"
|
||||
|
||||
"github.com/fatih/color"
|
||||
)
|
||||
@@ -55,7 +56,7 @@ func deleteAccount(vault *account.VaultWithDb) {
|
||||
if isDeleted {
|
||||
color.Green("Удалено")
|
||||
} else {
|
||||
color.Red("Не найдено")
|
||||
output.PrintError("Не найдено")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -66,7 +67,7 @@ func createAccount(vault *account.VaultWithDb) {
|
||||
|
||||
myAccount, err := account.NewAccount(login, password, url)
|
||||
if err != nil {
|
||||
fmt.Println("ОШИБКА: Неверный формат URL или Login")
|
||||
output.PrintError("ОШИБКА: Неверный формат URL или Login")
|
||||
return
|
||||
}
|
||||
vault.AddAccount(*myAccount)
|
||||
|
||||
16
go-demo-4/output/errors.go
Normal file
16
go-demo-4/output/errors.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package output
|
||||
|
||||
import "github.com/fatih/color"
|
||||
|
||||
func PrintError(value any) {
|
||||
switch t := value.(type) {
|
||||
case string:
|
||||
color.Red(t)
|
||||
case int:
|
||||
color.Red("Код ошибки: %d", t)
|
||||
case error:
|
||||
color.Red(t.Error())
|
||||
default:
|
||||
color.Red("Неизвестный тип ошибки")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user