-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttperrors.go
More file actions
37 lines (30 loc) · 807 Bytes
/
httperrors.go
File metadata and controls
37 lines (30 loc) · 807 Bytes
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
// Copyright 2021 Kévin José. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
package easyapi
import (
"fmt"
"github.com/gin-gonic/gin"
)
type httpError struct {
Code int `json:"code"`
Message string `json:"message"`
Data interface{} `json:"data"`
}
// Implements the Error interface
func (e *httpError) Error() string {
return fmt.Sprintf("%d - %s - %v", e.Code, e.Message, e.Data)
}
// Create a http error and display it on gin context
func HttpError(c *gin.Context, code int, message string, data interface{}) error {
if message == "" {
message = "An error occured"
}
e := &httpError{
Code: code,
Message: message,
Data: data,
}
c.JSON(code, gin.H{"error": e})
return e
}