forked from canonical/lxd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
smart_linux.go
46 lines (38 loc) · 911 Bytes
/
smart_linux.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//go:build linux && cgo && !agent
// +build linux,cgo,!agent
package response
import (
"database/sql"
"os"
"github.com/canonical/go-dqlite/driver"
"github.com/lxc/lxd/lxd/db"
"github.com/mattn/go-sqlite3"
"github.com/pkg/errors"
)
// SmartError returns the right error message based on err.
func SmartError(err error) Response {
if err == nil {
return EmptySyncResponse
}
switch errors.Cause(err) {
case os.ErrNotExist, sql.ErrNoRows, db.ErrNoSuchObject:
if errors.Cause(err) != err {
return NotFound(err)
}
return NotFound(nil)
case os.ErrPermission:
if errors.Cause(err) != err {
return Forbidden(err)
}
return Forbidden(nil)
case db.ErrAlreadyDefined, sqlite3.ErrConstraintUnique:
if errors.Cause(err) != err {
return Conflict(err)
}
return Conflict(nil)
case driver.ErrNoAvailableLeader:
return Unavailable(err)
default:
return InternalError(err)
}
}