-
Notifications
You must be signed in to change notification settings - Fork 0
/
pg.go
111 lines (105 loc) · 3.14 KB
/
pg.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package logger
import (
"fmt"
"log"
"reflect"
"strconv"
"strings"
pgx "github.com/lib/pq"
)
func ParsePG(err *pgx.Error) (outError *InformationConstruct) {
switch err.Code {
case "42702":
outError = BadRequest(err, err.Detail)
outError.Hint = err.Hint
outError.Message = err.Message
outError.Code = "42702"
case "23502":
outError = BadRequest(err, err.Detail)
outError.Hint = err.Hint
outError.Message = err.Message
outError.Code = "23502"
case "23505":
outError = BadRequest(err, err.Detail)
outError.Hint = err.Hint
outError.Message = err.Message
outError.Code = "23505"
case "42703": // Column not found error
outError = BadRequest(err, err.Routine)
outError.Hint = err.Hint
outError.Message = "This column does not appear to exist: " + strings.Split(err.Message, " ")[2]
outError.Code = "42307"
case "42601": // bad syntax error
outError = BadRequest(err, err.Routine)
outError.Hint = "Your syntax might be off, review all your column and table references."
outError.Message = err.Message
outError.Code = "42601"
case "22P02": // bad syntax error for UUID
outError = BadRequest(err, err.Routine)
outError.Hint = "You have an inalid PRIMARY ID in your database transaction"
outError.Message = err.Message
outError.Code = "22P02"
case "42P01": // table not found
outError = BadRequest(err, err.Routine)
outError.Hint = "You are trying to interact with a table that does not exist, double check your table names"
outError.Message = "This table does not appear to exist: " + strings.Split(err.Message, " ")[2]
outError.Code = "42P01"
case "42701":
outError = BadRequest(err, err.Routine)
outError.Message = err.Message
outError.Code = "42701"
case "42P18":
outError = BadRequest(err, err.Routine)
outError.Message = err.Message
outError.Code = "42P18"
outError.Hint = "Some of your query parameters might be invalid."
default:
// this is away to catch errors that are not supported.
// so that they can be added.
PrintObject(err)
}
return
}
func ParseDBError(er error) (outError *InformationConstruct) {
if er == nil {
return nil
}
switch er.(type) {
case *pgx.Error:
outError = ParsePG(er.(*pgx.Error))
// case "sql":
// todo for emil
default:
// some errors are going to get triggered here...
newErr := GenericError(er)
newErr.Message = er.Error()
newErr.HTTPCode = 404
return newErr
}
return
}
func PrintObject(Object interface{}) {
fields := reflect.TypeOf(Object).Elem()
values := reflect.ValueOf(Object).Elem()
num := fields.NumField()
parseFields(num, fields, values)
}
func parseFields(num int, fields reflect.Type, values reflect.Value) {
log.Println("!!!!!!!!!! UN-HANDLED POSTGRES ERROR !!!!!!!!!!")
for i := 0; i < num; i++ {
value := values.Field(i)
field := fields.Field(i)
switch value.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
valueInt := strconv.FormatInt(value.Int(), 64)
if valueInt != "" {
fmt.Println(field.Name, valueInt)
}
case reflect.String:
if value.String() != "" {
fmt.Println(field.Name, value.String())
}
}
}
log.Println("!!!!!!!!!! UN-HANDLED POSTGRES ERROR !!!!!!!!!!")
}