Skip to content

Commit

Permalink
Backport removal of non- stdlib errors from lxd/response (stable-5.…
Browse files Browse the repository at this point in the history
…21) (#14411)

This backports #14408.
  • Loading branch information
tomponline authored Nov 6, 2024
2 parents f0be5c8 + 9106f5b commit 4aab50e
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 19 deletions.
10 changes: 10 additions & 0 deletions lxd/db/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,19 @@ package db

import (
"fmt"
"net/http"

"github.com/canonical/go-dqlite/v2/driver"
"github.com/mattn/go-sqlite3"
)

var (
// ErrNoClusterMember is used to indicate no cluster member has been found for a resource.
ErrNoClusterMember = fmt.Errorf("No cluster member found")
)

// SmartErrors are used to return more appropriate errors to the caller.
var SmartErrors = map[int][]error{
http.StatusConflict: {sqlite3.ErrConstraintUnique},
http.StatusServiceUnavailable: {driver.ErrNoAvailableLeader},
}
3 changes: 2 additions & 1 deletion lxd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/spf13/cobra"

"github.com/canonical/lxd/lxd/daemon"
"github.com/canonical/lxd/lxd/db"
"github.com/canonical/lxd/lxd/events"
"github.com/canonical/lxd/lxd/operations"
"github.com/canonical/lxd/lxd/response"
Expand Down Expand Up @@ -54,7 +55,7 @@ func (c *cmdGlobal) Run(cmd *cobra.Command, args []string) error {
operations.Init(daemon.Debug)

// Set debug for the response package
response.Init(daemon.Debug)
response.Init(daemon.Debug, db.SmartErrors)

// Setup logger
syslog := ""
Expand Down
14 changes: 12 additions & 2 deletions lxd/response/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,19 @@ import (

var debug bool

// Init sets the debug variable to the provided value.
func Init(d bool) {
// Init sets the debug variable to the provided value and registers any additional smart error mappings.
func Init(d bool, smartErrors map[int][]error) {
debug = d

for code, additionalErrors := range smartErrors {
existingErrs, ok := httpResponseErrors[code]
if ok {
httpResponseErrors[code] = append(existingErrs, additionalErrors...)
continue
}

httpResponseErrors[code] = additionalErrors
}
}

// Response represents an API response.
Expand Down
16 changes: 0 additions & 16 deletions lxd/response/smart_linux.go

This file was deleted.

0 comments on commit 4aab50e

Please sign in to comment.