Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
dtd
/
go-webserver-boilerplate
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit 53822884
authored
Feb 26, 2025
by
mhmdhaekal
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix(initialize): use default handler if it's gorm error
1 parent
603f30f0
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
17 additions
and
3 deletions
internal/domain/error.go
internal/handler/error.go
internal/domain/error.go
View file @
5382288
...
...
@@ -19,6 +19,7 @@ var (
Validation
=
ErrorCode
{
"validation_error"
}
Database
=
ErrorCode
{
"database_error"
}
InactiveUser
=
ErrorCode
{
"user_inactive"
}
Unauthorized
=
ErrorCode
{
"unauthorized"
}
)
func
FromString
(
s
string
)
(
ErrorCode
,
error
)
{
...
...
@@ -69,6 +70,10 @@ var (
Code
:
InactiveUser
,
Message
:
"user account is inactive"
,
}
ErrUnauthorized
=
&
Error
{
Code
:
Unauthorized
,
Message
:
"Unauthorized"
,
}
)
func
NewError
(
code
ErrorCode
,
message
string
,
err
error
)
*
Error
{
...
...
internal/handler/error.go
View file @
5382288
...
...
@@ -13,7 +13,16 @@ func ErrorHandler(ctx fiber.Ctx, err error) error {
code
:=
fiber
.
StatusInternalServerError
if
customErr
,
ok
:=
err
.
(
*
domain
.
Error
);
ok
{
var
fiberErr
*
fiber
.
Error
if
errors
.
As
(
err
,
&
fiberErr
)
{
code
=
fiberErr
.
Code
response
:=
NewBaseResponse
(
code
,
nil
,
err
)
return
ctx
.
Status
(
code
)
.
JSON
(
response
)
}
var
customErr
*
domain
.
Error
if
errors
.
As
(
err
,
&
customErr
)
{
switch
customErr
.
Code
{
case
domain
.
NotFound
:
code
=
fiber
.
StatusNotFound
...
...
@@ -23,11 +32,11 @@ func ErrorHandler(ctx fiber.Ctx, err error) error {
code
=
fiber
.
StatusInternalServerError
case
domain
.
InactiveUser
:
code
=
fiber
.
StatusForbidden
case
domain
.
Unauthorized
:
code
=
fiber
.
StatusUnauthorized
default
:
code
=
fiber
.
StatusInternalServerError
}
}
else
if
fiberErr
,
ok
:=
err
.
(
*
fiber
.
Error
);
ok
{
code
=
fiberErr
.
Code
}
response
:=
NewBaseResponse
(
code
,
nil
,
err
)
...
...
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment