Skip to content

Commit

Permalink
feat: update error struct and OpenAPI specs (#312)
Browse files Browse the repository at this point in the history
Co-authored-by: brucexc <[email protected]>
Co-authored-by: Henry <[email protected]>
  • Loading branch information
3 people authored May 29, 2024
1 parent fd01039 commit c818e11
Show file tree
Hide file tree
Showing 10 changed files with 745 additions and 44 deletions.
30 changes: 12 additions & 18 deletions common/http/response/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,40 +12,34 @@ type ErrorCode int

const (
ErrorCodeBadRequest ErrorCode = iota + 1
ErrorCodeValidateFailed
ErrorCodeValidationFailed
ErrorCodeBadParams
ErrorCodeInternalError
)

type ErrorResponse struct {
Error string `json:"error"`
ErrorCode ErrorCode `json:"error_code"`
Message string `json:"message"`
Code ErrorCode `json:"code"`
}

func BadRequestError(c echo.Context, err error) error {
return c.JSON(http.StatusBadRequest, &ErrorResponse{
ErrorCode: ErrorCodeBadRequest,
Error: fmt.Sprintf("Please check your request and try again, %s", err),
Code: ErrorCodeBadRequest,
Message: fmt.Sprintf("Please check your request and try again, %s", err),
})
}

func ValidateFailedError(c echo.Context, err error) error {
func ValidationFailedError(c echo.Context, err error) error {
return c.JSON(http.StatusBadRequest, &ErrorResponse{
ErrorCode: ErrorCodeValidateFailed,
Error: fmt.Sprintf("Please check your request validation and try again, %s", err),
Code: ErrorCodeValidationFailed,
Message: fmt.Sprintf("Please check your request validation and try again, %s", err),
})
}

func BadParamsError(c echo.Context, err error) error {
return c.JSON(http.StatusBadRequest, &ErrorResponse{
ErrorCode: ErrorCodeBadParams,
Error: fmt.Sprintf("Please check your param combination and try again, %s", err),
})
}

func InternalError(c echo.Context, err error) error {
// InternalError should not return the details of the error to the client for safety reasons.
func InternalError(c echo.Context) error {
return c.JSON(http.StatusInternalServerError, &ErrorResponse{
ErrorCode: ErrorCodeInternalError,
Error: fmt.Sprintf("An internal error has occurred, please try again later, %s", err),
Code: ErrorCodeInternalError,
Message: "An internal error has occurred, please try again later",
})
}
28 changes: 14 additions & 14 deletions common/http/response/error_code.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

123 changes: 123 additions & 0 deletions docs/openapi.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package docs

import (
"fmt"
"os"
"sort"

"github.com/rss3-network/node/schema/worker"
"github.com/rss3-network/protocol-go/schema"
"github.com/rss3-network/protocol-go/schema/activity"
"github.com/rss3-network/protocol-go/schema/network"
"github.com/rss3-network/protocol-go/schema/tag"
"github.com/samber/lo"
"github.com/tidwall/sjson"
"go.uber.org/zap"
)

var FilePath = "./docs/openapi.json"

func Generate(endpoint string) ([]byte, error) {
// Read the existing openapi.json file
file, err := os.ReadFile(FilePath)
if err != nil {
zap.L().Error("read file error", zap.Error(err))

return nil, err
}

// Generate servers.
if file, err = generateServers(file, endpoint); err != nil {
return nil, err
}

// Generate network, tag, platform, direction enum.
if file, err = generateEnum(file); err != nil {
return nil, err
}

return file, nil
}

func generateServers(file []byte, endpoint string) ([]byte, error) {
var err error

file, err = sjson.SetBytes(file, "servers", []map[string]interface{}{
{"url": endpoint, "description": "Node Endpoint"},
{"url": "http://localhost", "description": "Localhost"},
})
if err != nil {
zap.L().Error("sjson set servers error", zap.Error(err))

return nil, err
}

return file, nil
}

func generateEnum(file []byte) ([]byte, error) {
var err error

// Generate network values.
networks := lo.Filter(network.NetworkStrings(), func(s string, _ int) bool {
return !lo.Contains([]string{
network.Unknown.String(),
network.Bitcoin.String(),
network.SatoshiVM.String(),
network.RSS.String(),
}, s)
})

sort.Strings(networks)

file, err = sjson.SetBytes(file, "components.schemas.Network.enum", networks)
if err != nil {
return nil, fmt.Errorf("sjson set network enum err: %w", err)
}

// Generate tag values.
tags := tag.TagStrings()

sort.Strings(tags)

file, err = sjson.SetBytes(file, "components.schemas.Tag.enum", tags)
if err != nil {
return nil, fmt.Errorf("sjson set tag enum err: %w", err)
}

// Generate platform values.
platforms := worker.PlatformStrings()

sort.Strings(platforms)

file, err = sjson.SetBytes(file, "components.schemas.Platform.enum", platforms)
if err != nil {
return nil, fmt.Errorf("sjson set platform enum err: %w", err)
}

// Generate direction values.
file, err = sjson.SetBytes(file, "components.schemas.Direction.enum", activity.DirectionStrings())
if err != nil {
return nil, fmt.Errorf("sjson set direction enum err: %w", err)
}

// Generate type values.
types := make([]string, 0)

for _, v := range tag.TagValues() {
for _, t := range schema.GetTypesByTag(v) {
types = append(types, t.Name())
}
}

types = lo.Uniq(types)

sort.Strings(types)

file, err = sjson.SetBytes(file, "components.schemas.Type.enum", types)
if err != nil {
return nil, fmt.Errorf("sjson set type enum err: %w", err)
}

return file, nil
}
Loading

0 comments on commit c818e11

Please sign in to comment.