errors handler
This commit is contained in:
parent
9752e7d9ae
commit
61395ab61b
@ -2,12 +2,14 @@ package models
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"errors"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
dns "github.com/cloud66-oss/coredns_mysql"
|
dns "github.com/cloud66-oss/coredns_mysql"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var ErrorZoneNotEndWithDot = errors.New("zone should end with '.'")
|
||||||
|
|
||||||
const (
|
const (
|
||||||
RecordTypeA = "A"
|
RecordTypeA = "A"
|
||||||
RecordTypeAAAA = "AAAA"
|
RecordTypeAAAA = "AAAA"
|
||||||
@ -41,7 +43,7 @@ func (Record[T]) TableName() string {
|
|||||||
|
|
||||||
func (r Record[T]) CheckZone() error {
|
func (r Record[T]) CheckZone() error {
|
||||||
if strings.HasSuffix(r.Zone, ".") {
|
if strings.HasSuffix(r.Zone, ".") {
|
||||||
return fmt.Errorf("zone should end with '.'")
|
return ErrorZoneNotEndWithDot
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@ package server
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"reCoreD-UI/models"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
@ -17,16 +18,21 @@ type Response struct {
|
|||||||
|
|
||||||
func errorHandler(c *gin.Context, err error) {
|
func errorHandler(c *gin.Context, err error) {
|
||||||
logrus.Error(err)
|
logrus.Error(err)
|
||||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
switch {
|
||||||
|
case errors.Is(err, gorm.ErrRecordNotFound):
|
||||||
c.JSON(http.StatusNotFound, Response{
|
c.JSON(http.StatusNotFound, Response{
|
||||||
Succeed: false,
|
Succeed: false,
|
||||||
Message: err.Error(),
|
Message: err.Error(),
|
||||||
})
|
})
|
||||||
} else {
|
case errors.Is(err, models.ErrorZoneNotEndWithDot):
|
||||||
|
c.JSON(http.StatusBadRequest, Response{
|
||||||
|
Succeed: false,
|
||||||
|
Message: err.Error(),
|
||||||
|
})
|
||||||
|
default:
|
||||||
c.JSON(http.StatusInternalServerError, Response{
|
c.JSON(http.StatusInternalServerError, Response{
|
||||||
Succeed: false,
|
Succeed: false,
|
||||||
Message: err.Error(),
|
Message: err.Error(),
|
||||||
Data: nil,
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user