Browse Source

ErrorHandler resolvido

William Wiechorek 6 years ago
parent
commit
faac27e31c
5 changed files with 29 additions and 28 deletions
  1. BIN
      build
  2. 15 16
      core/errors/errors.go
  3. 4 3
      main.go
  4. 3 3
      routes.go
  5. 7 6
      src/controller/Test/Test.go

BIN
build


+ 15 - 16
core/errors/errors.go

1
-package errors
1
+package errorHandler
2
 
2
 
3
 const INVALID_PARAMETER = 400
3
 const INVALID_PARAMETER = 400
4
 const ACTION_FORBIDDEN = 401
4
 const ACTION_FORBIDDEN = 401
16
 	Message   string `json:"message"`
16
 	Message   string `json:"message"`
17
 }
17
 }
18
 
18
 
19
-var errors []interface{}
19
+type Errors struct {
20
+	Errors []interface{} `json:"errors"`
21
+}
20
 
22
 
21
-func InvalidParameter(parameter string, message string) {
23
+func (err *Errors) InvalidParameter(parameter string, message string) {
22
 	var e = apiErrorParameter{
24
 	var e = apiErrorParameter{
23
 		"invalid_parameter",
25
 		"invalid_parameter",
24
 		parameter,
26
 		parameter,
25
 		message,
27
 		message,
26
 	}
28
 	}
27
 
29
 
28
-	errors = append(errors, e)
30
+	err.Errors = append(err.Errors, e)
29
 }
31
 }
30
 
32
 
31
-func ActionForbidden(message string) {
33
+func (err *Errors) ActionForbidden(message string) {
32
 	var e = apiError{
34
 	var e = apiError{
33
 		"action_forbidden",
35
 		"action_forbidden",
34
 		message,
36
 		message,
35
 	}
37
 	}
36
 
38
 
37
-	errors = append(errors, e)
39
+	err.Errors = append(err.Errors, e)
38
 }
40
 }
39
 
41
 
40
-func InternalError(message string) {
42
+func (err *Errors) InternalError(message string) {
41
 	var e = apiError{
43
 	var e = apiError{
42
 		"internal_error",
44
 		"internal_error",
43
 		message,
45
 		message,
44
 	}
46
 	}
45
 
47
 
46
-	errors = append(errors, e)
48
+	err.Errors = append(err.Errors, e)
49
+
47
 }
50
 }
48
 
51
 
49
-func NotFound(message string) {
52
+func (err *Errors) NotFound(message string) {
50
 	var e = apiError{
53
 	var e = apiError{
51
 		"not_found",
54
 		"not_found",
52
 		message,
55
 		message,
53
 	}
56
 	}
54
 
57
 
55
-	errors = append(errors, e)
56
-}
57
-
58
-func Has() bool {
59
-	return len(errors) > 0
58
+	err.Errors = append(err.Errors, e)
60
 }
59
 }
61
 
60
 
62
-func Get() []interface{} {
63
-	return errors
61
+func (err *Errors) Has() bool {
62
+	return len(err.Errors) > 0
64
 }
63
 }

+ 4 - 3
main.go

6
 	"strconv"
6
 	"strconv"
7
 
7
 
8
 	"./core/database"
8
 	"./core/database"
9
-	"./core/errors"
9
+	"./core/errorHandler"
10
 	req "./core/request"
10
 	req "./core/request"
11
 	res "./core/response"
11
 	res "./core/response"
12
 	"./src/lib/auth"
12
 	"./src/lib/auth"
18
 	initAuthService()
18
 	initAuthService()
19
 
19
 
20
 	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
20
 	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
21
+		var Errors errorHandler.Errors
21
 
22
 
22
 		res.SetResponseWriter(w)
23
 		res.SetResponseWriter(w)
23
 		req.SetRequest(r)
24
 		req.SetRequest(r)
24
 
25
 
25
 		if !database.IsConnected {
26
 		if !database.IsConnected {
26
-			errors.InternalError("Conexão com a base de dados falhou!")
27
-			res.JSON(errors.Get())
27
+			Errors.InternalError("Conexão com a base de dados falhou!")
28
+			res.JSON(Errors)
28
 			return
29
 			return
29
 		}
30
 		}
30
 
31
 

+ 3 - 3
routes.go

11
 	router.Route{
11
 	router.Route{
12
 		Pattern: `\/(.*)`,
12
 		Pattern: `\/(.*)`,
13
 		Handler: test.Inicial,
13
 		Handler: test.Inicial,
14
-		Middlewares: [
15
-			login.Logged
16
-		]
14
+		Middlewares: []router.Middleware{
15
+			login.Logged,
16
+		},
17
 	},
17
 	},
18
 }
18
 }

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

4
 	"fmt"
4
 	"fmt"
5
 	"net/http"
5
 	"net/http"
6
 
6
 
7
-	"../../../core/errors"
7
+	"../../../core/errorHandler"
8
 	"../../../core/request"
8
 	"../../../core/request"
9
 	"../../../core/response"
9
 	"../../../core/response"
10
 
10
 
18
 
18
 
19
 //Inicial : Func
19
 //Inicial : Func
20
 func Inicial(w http.ResponseWriter, r *http.Request, vars []string) {
20
 func Inicial(w http.ResponseWriter, r *http.Request, vars []string) {
21
+	var Errors errorHandler.Errors
21
 
22
 
22
 	user, err := auth.GetUser(r)
23
 	user, err := auth.GetUser(r)
23
 
24
 
34
 		return
35
 		return
35
 	}
36
 	}
36
 
37
 
37
-	errors.InvalidParameter("username", "Usuário não disponível")
38
-	errors.InternalError("Ocorreu um erro interno")
39
-	errors.ActionForbidden("Ocorreu um erro interno2")
38
+	Errors.InvalidParameter("username", "Usuário não disponível")
39
+	Errors.InternalError("Ocorreu um erro interno")
40
+	Errors.ActionForbidden("Ocorreu um erro interno2")
40
 
41
 
41
-	if errors.Has() {
42
-		response.JSON(errors.Get())
42
+	if Errors.Has() {
43
+		response.JSON(Errors)
43
 		return
44
 		return
44
 	}
45
 	}
45
 
46