refactor: unify error handling

This commit is contained in:
2025-09-03 21:09:42 +02:00
parent 6db6127522
commit 863171f66b
6 changed files with 54 additions and 40 deletions

View File

@@ -50,13 +50,13 @@ func generateSelfSignedTLSCertificate() error {
derBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, &privateKey.PublicKey, privateKey)
if err != nil {
return err
return fmt.Errorf("Failed to create new x509 certificate: %v", err)
}
cert := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: derBytes})
key, err := x509.MarshalECPrivateKey(privateKey)
if err != nil {
return err
return fmt.Errorf("Failed to marshal EC private key: %v", err)
}
keyPEM := pem.EncodeToMemory(&pem.Block{Type: "EC PRIVATE KEY", Bytes: key})
@@ -77,17 +77,17 @@ func loadOrGenerateTLSCertificate() error {
_, err := tls.LoadX509KeyPair(pathTLSCertFile, pathTLSKeyFile)
if err != nil {
return fmt.Errorf("Error: Failed to load TLS certificate or key: %w", err)
return fmt.Errorf("Failed to load TLS certificate or key: %w", err)
}
certData, err := os.ReadFile(pathTLSCertFile)
if err != nil {
return fmt.Errorf("Error: Failed to read TLS certificate file: %w", err)
return fmt.Errorf("Failed to read TLS certificate file: %w", err)
}
keyData, err := os.ReadFile(pathTLSKeyFile)
if err != nil {
return fmt.Errorf("Error: Failed to read TLS key file: %w", err)
return fmt.Errorf("Failed to read TLS key file: %w", err)
}
selfSignedTLSCertificate = certData

View File

@@ -2,7 +2,6 @@ package config
import (
"fmt"
"os"
)
const DefaultBasicAuthUsername string = "ablage"
@@ -14,22 +13,22 @@ const VersionString string = "1.1"
var randomBasicAuthPassword string = generateRandomPassword()
func Init() {
func Init() error {
err := gatherDefaultPaths()
if err != nil {
panic(err)
return err
}
parseFlags()
if GetReadonlyMode() && GetSinkholeMode() {
fmt.Println("Error: Cannot enable both readonly and sinkhole modes at the same time.")
os.Exit(1)
return fmt.Errorf("Cannot enable both readonly and sinkhole modes at the same time.")
}
err = loadOrGenerateTLSCertificate()
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
return err
}
return nil
}

View File

@@ -12,7 +12,7 @@ var defaultPathUploadFolder string = ""
func gatherDefaultPaths() error {
execPath, err := os.Executable()
if err != nil {
return fmt.Errorf("[Error] Could not determine binary path: %v", err)
return fmt.Errorf("Could not determine binary path: %v", err)
}
defaultPathDataFolder = filepath.Join(filepath.Dir(execPath), DefaultNameDataFolder)