Browse Source
feat: standardize error response (#48)
feat: standardize error response (#48)
* feat: update openapi * feat: standardize errorpull/49/head v4.4.1
committed by
GitHub
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 262 additions and 125 deletions
-
187docs/openapi.yaml
-
1src/go.mod
-
3src/go.sum
-
6src/internal/rest/app.go
-
3src/internal/rest/middleware/recovery.go
-
9src/internal/rest/send.go
-
6src/internal/rest/user.go
-
108src/pkg/error/app_error.go
-
19src/pkg/error/auth_error.go
-
18src/pkg/error/whatsapp_error.go
-
4src/pkg/utils/response.go
-
23src/services/app.go
@ -0,0 +1,108 @@ |
|||
package error |
|||
|
|||
import "net/http" |
|||
|
|||
type LoginError string |
|||
|
|||
// Error for complying the error interface
|
|||
func (e LoginError) Error() string { |
|||
return string(e) |
|||
} |
|||
|
|||
// ErrCode will return the error code based on the error data type
|
|||
func (e LoginError) ErrCode() string { |
|||
return "ALREADY_LOGGED_IN" |
|||
} |
|||
|
|||
// StatusCode will return the HTTP status code based on the error data type
|
|||
func (e LoginError) StatusCode() int { |
|||
return http.StatusBadRequest |
|||
} |
|||
|
|||
type ReconnectError string |
|||
|
|||
func throwReconnectError(text string) GenericError { |
|||
return AuthError(text) |
|||
} |
|||
|
|||
// Error for complying the error interface
|
|||
func (e ReconnectError) Error() string { |
|||
return string(e) |
|||
} |
|||
|
|||
// ErrCode will return the error code based on the error data type
|
|||
func (e ReconnectError) ErrCode() string { |
|||
return "RECONNECT_ERROR" |
|||
} |
|||
|
|||
// StatusCode will return the HTTP status code based on the error data type
|
|||
func (e ReconnectError) StatusCode() int { |
|||
return http.StatusBadRequest |
|||
} |
|||
|
|||
type AuthError string |
|||
|
|||
func throwAuthError(text string) GenericError { |
|||
return AuthError(text) |
|||
} |
|||
|
|||
func (err AuthError) Error() string { |
|||
return string(err) |
|||
} |
|||
|
|||
// ErrCode will return the error code based on the error data type
|
|||
func (err AuthError) ErrCode() string { |
|||
return "AUTHENTICATION_ERROR" |
|||
} |
|||
|
|||
// StatusCode will return the HTTP status code based on the error data type
|
|||
func (err AuthError) StatusCode() int { |
|||
return http.StatusUnauthorized |
|||
} |
|||
|
|||
type qrChannelError string |
|||
|
|||
func throwQrChannelError(text string) GenericError { |
|||
return qrChannelError(text) |
|||
} |
|||
|
|||
func (err qrChannelError) Error() string { |
|||
return string(err) |
|||
} |
|||
|
|||
// ErrCode will return the error code based on the error data type
|
|||
func (err qrChannelError) ErrCode() string { |
|||
return "QR_CHANNEL_ERROR" |
|||
} |
|||
|
|||
// StatusCode will return the HTTP status code based on the error data type
|
|||
func (err qrChannelError) StatusCode() int { |
|||
return http.StatusInternalServerError |
|||
} |
|||
|
|||
type sessionSavedError string |
|||
|
|||
func throwSessionSavedError(text string) GenericError { |
|||
return sessionSavedError(text) |
|||
} |
|||
|
|||
func (err sessionSavedError) Error() string { |
|||
return string(err) |
|||
} |
|||
|
|||
// ErrCode will return the error code based on the error data type
|
|||
func (err sessionSavedError) ErrCode() string { |
|||
return "SESSION_SAVED_ERROR" |
|||
} |
|||
|
|||
// StatusCode will return the HTTP status code based on the error data type
|
|||
func (err sessionSavedError) StatusCode() int { |
|||
return http.StatusInternalServerError |
|||
} |
|||
|
|||
var ( |
|||
ErrAlreadyLoggedIn = LoginError("You already logged in :)") |
|||
ErrReconnect = throwReconnectError("Reconnect error") |
|||
ErrQrChannel = throwQrChannelError("QR channel error") |
|||
ErrorSessionSaved = throwSessionSavedError("Your session have been saved, please wait to connect 2 second and refresh again") |
|||
) |
|||
@ -1,19 +0,0 @@ |
|||
package error |
|||
|
|||
import "net/http" |
|||
|
|||
type AuthError string |
|||
|
|||
func (err AuthError) Error() string { |
|||
return string(err) |
|||
} |
|||
|
|||
// ErrCode will return the error code based on the error data type
|
|||
func (err AuthError) ErrCode() string { |
|||
return "AUTHENTICATION_ERROR" |
|||
} |
|||
|
|||
// StatusCode will return the HTTP status code based on the error data type
|
|||
func (err AuthError) StatusCode() int { |
|||
return http.StatusUnauthorized |
|||
} |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue