Matheus Querido преди 6 години
родител
ревизия
ca788eb9a9
променени са 4 файла, в които са добавени 68 реда и са изтрити 73 реда
  1. BIN
      debug
  2. 8 7
      src/controller/Test/Test.go
  3. 0 66
      src/lib/error/error.go
  4. 60 0
      src/lib/errors/errors.go

+ 8 - 7
src/controller/Test/Test.go

@@ -8,7 +8,7 @@ import (
8 8
 	res "../../../core/response"
9 9
 
10 10
 	auth "../../lib/auth"
11
-	errorHandler "../../lib/error"
11
+	"../../lib/errors"
12 12
 )
13 13
 
14 14
 type Profile struct {
@@ -24,7 +24,7 @@ func Inicial(w http.ResponseWriter, r *http.Request, vars []string) {
24 24
 	fmt.Printf("%+v\n", user)
25 25
 	fmt.Printf("%+v\n", err)
26 26
 
27
-	var Errors errorHandler.Errors
27
+	//var errors errors.Errors
28 28
 	// fmt.Println(vars[0])
29 29
 	// fmt.Fprintf(w, "Hello Route, %q", html.EscapeString(r.URL.Path))
30 30
 	// profile := Profile{"Alex", []string{"snowboarding", "programming"}}
@@ -34,11 +34,12 @@ func Inicial(w http.ResponseWriter, r *http.Request, vars []string) {
34 34
 		return
35 35
 	}
36 36
 
37
-	Errors.InvalidParameter("username", "Usuário não disponível")
38
-	Errors.InternalError("Ocorreu um erro interno")
39
-	Errors.ActionForbidden("Ocorreu um erro interno2")
40
-	if Errors.Has() {
41
-		res.JSON(Errors)
37
+	errors.InvalidParameter("username", "Usuário não disponível")
38
+	errors.InternalError("Ocorreu um erro interno")
39
+	errors.ActionForbidden("Ocorreu um erro interno2")
40
+
41
+	if !errors.Has() {
42
+		res.JSON(errors)
42 43
 		return
43 44
 	}
44 45
 

+ 0 - 66
src/lib/error/error.go

@@ -1,66 +0,0 @@
1
-package error
2
-
3
-const InvalidParameter = 400
4
-const ActionForbidden = 401
5
-const NotFound = 404
6
-const InternalError = 500
7
-
8
-type apiErrorParameter struct {
9
-	ErrorType string `json:"type"`
10
-	Parameter string `json:"parameter"`
11
-	Message   string `json:"message"`
12
-}
13
-
14
-type apiError struct {
15
-	ErrorType string `json:"type"`
16
-	Message   string `json:"message"`
17
-}
18
-
19
-type Errors struct {
20
-	Errors []interface{} `json:"errors"`
21
-}
22
-
23
-func (err *Errors) InvalidParameter(parameter string, message string) {
24
-	var e = apiErrorParameter{
25
-		"invalid_parameter",
26
-		parameter,
27
-		message,
28
-	}
29
-
30
-	err.Errors = append(err.Errors, e)
31
-}
32
-
33
-func (err *Errors) ActionForbidden(message string) {
34
-	var e = apiError{
35
-		"action_forbidden",
36
-		message,
37
-	}
38
-
39
-	err.Errors = append(err.Errors, e)
40
-}
41
-
42
-func (err *Errors) InternalError(message string) {
43
-	var e = apiError{
44
-		"internal_error",
45
-		message,
46
-	}
47
-
48
-	err.Errors = append(err.Errors, e)
49
-}
50
-
51
-func (err *Errors) NotFound(message string) {
52
-	var e = apiError{
53
-		"not_found",
54
-		message,
55
-	}
56
-
57
-	err.Errors = append(err.Errors, e)
58
-}
59
-
60
-func (err *Errors) Has() bool {
61
-	if len(err.Errors) > 0 {
62
-		return true
63
-	}
64
-
65
-	return false
66
-}

+ 60 - 0
src/lib/errors/errors.go

@@ -0,0 +1,60 @@
1
+package errors
2
+
3
+const INVALID_PARAMETER = 400
4
+const ACTION_FORBIDDEN = 401
5
+const NOT_FOUND = 404
6
+const INTERNAL_ERROR = 500
7
+
8
+type apiErrorParameter struct {
9
+	ErrorType string `json:"type"`
10
+	Parameter string `json:"parameter"`
11
+	Message   string `json:"message"`
12
+}
13
+
14
+type apiError struct {
15
+	ErrorType string `json:"type"`
16
+	Message   string `json:"message"`
17
+}
18
+
19
+var errors []interface{}
20
+
21
+func InvalidParameter(parameter string, message string) {
22
+	var e = apiErrorParameter{
23
+		"invalid_parameter",
24
+		parameter,
25
+		message,
26
+	}
27
+
28
+	errors = append(errors, e)
29
+}
30
+
31
+func ActionForbidden(message string) {
32
+	var e = apiError{
33
+		"action_forbidden",
34
+		message,
35
+	}
36
+
37
+	errors = append(errors, e)
38
+}
39
+
40
+func InternalError(message string) {
41
+	var e = apiError{
42
+		"internal_error",
43
+		message,
44
+	}
45
+
46
+	errors = append(errors, e)
47
+}
48
+
49
+func NotFound(message string) {
50
+	var e = apiError{
51
+		"not_found",
52
+		message,
53
+	}
54
+
55
+	errors = append(errors, e)
56
+}
57
+
58
+func Has() bool {
59
+	return len(errors) <= 0
60
+}