Skip to content
This repository has been archived by the owner on Aug 16, 2022. It is now read-only.

Commit

Permalink
refactor: pkg/error (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
rot1024 committed Jul 30, 2021
1 parent 78ac136 commit a3f8b6d
Show file tree
Hide file tree
Showing 57 changed files with 514 additions and 339 deletions.
6 changes: 3 additions & 3 deletions internal/app/app.go
Expand Up @@ -8,8 +8,8 @@ import (
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/reearth/reearth-backend/internal/adapter/graphql"
err1 "github.com/reearth/reearth-backend/pkg/error"
"github.com/reearth/reearth-backend/pkg/log"
"github.com/reearth/reearth-backend/pkg/rerror"
echotracer "go.opentelemetry.io/contrib/instrumentation/github.com/labstack/echo"
)

Expand Down Expand Up @@ -116,11 +116,11 @@ func errorMessage(err error, log func(string, ...interface{})) (int, string) {
if err2.Internal != nil {
log("echo internal err: %+v", err2)
}
} else if errors.Is(err, err1.ErrNotFound) {
} else if errors.Is(err, rerror.ErrNotFound) {
code = http.StatusNotFound
msg = "not found"
} else {
var ierr *err1.ErrInternal
var ierr *rerror.ErrInternal
if errors.As(err, &ierr) {
if err2 := ierr.Unwrap(); err2 != nil {
log("internal err: %+v", err2)
Expand Down
10 changes: 5 additions & 5 deletions internal/app/auth.go
Expand Up @@ -6,8 +6,8 @@ import (
"github.com/labstack/echo/v4"
"github.com/reearth/reearth-backend/internal/graphql"
"github.com/reearth/reearth-backend/internal/usecase"
err1 "github.com/reearth/reearth-backend/pkg/error"
"github.com/reearth/reearth-backend/pkg/id"
"github.com/reearth/reearth-backend/pkg/rerror"
"github.com/reearth/reearth-backend/pkg/user"
)

Expand Down Expand Up @@ -46,7 +46,7 @@ func authMiddleware(cfg *ServerConfig) echo.MiddlewareFunc {
if u == nil && userID != "" {
if userID2, err := id.UserIDFrom(userID); err == nil {
u, err = cfg.Repos.User.FindByID(ctx, userID2)
if err != nil && err != err1.ErrNotFound {
if err != nil && err != rerror.ErrNotFound {
return err
}
} else {
Expand All @@ -58,7 +58,7 @@ func authMiddleware(cfg *ServerConfig) echo.MiddlewareFunc {
var err error
// find user
u, err = cfg.Repos.User.FindByAuth0Sub(ctx, sub)
if err != nil && err != err1.ErrNotFound {
if err != nil && err != rerror.ErrNotFound {
return err
}

Expand All @@ -78,11 +78,11 @@ func authMiddleware(cfg *ServerConfig) echo.MiddlewareFunc {
// // }

// u, err = cfg.Repos.User.FindByEmail(ctx, data.Email)
// if err != nil && err != err1.ErrNotFound {
// if err != nil && err != rerror.ErrNotFound {
// return err
// }
// if u == nil {
// return err1.ErrUserNotFound
// return rerror.ErrUserNotFound
// }
// }
}
Expand Down
4 changes: 2 additions & 2 deletions internal/app/file.go
Expand Up @@ -8,8 +8,8 @@ import (

"github.com/labstack/echo/v4"
"github.com/reearth/reearth-backend/internal/usecase/gateway"
err1 "github.com/reearth/reearth-backend/pkg/error"
"github.com/reearth/reearth-backend/pkg/id"
"github.com/reearth/reearth-backend/pkg/rerror"
)

func serveFiles(
Expand Down Expand Up @@ -51,7 +51,7 @@ func serveFiles(
fileHandler(func(ctx echo.Context) (io.Reader, string, error) {
pid, err := id.PluginIDFrom(ctx.Param("name") + "#" + ctx.Param("version"))
if err != nil {
return nil, "", err1.ErrNotFound
return nil, "", rerror.ErrNotFound
}
filename := ctx.Param("filename")
r, err := repo.ReadPluginFile(ctx.Request().Context(), pid, filename)
Expand Down
4 changes: 2 additions & 2 deletions internal/app/graphql.go
Expand Up @@ -18,7 +18,7 @@ import (
infra_graphql "github.com/reearth/reearth-backend/internal/graphql"
"github.com/reearth/reearth-backend/internal/graphql/dataloader"
"github.com/reearth/reearth-backend/internal/usecase"
err1 "github.com/reearth/reearth-backend/pkg/error"
"github.com/reearth/reearth-backend/pkg/rerror"
)

const enableDataLoaders = true
Expand Down Expand Up @@ -105,7 +105,7 @@ func graphqlAPI(
// show more detailed error messgage in debug mode
func(ctx context.Context, e error) *gqlerror.Error {
if conf.Debug {
var ierr *err1.ErrInternal
var ierr *rerror.ErrInternal
if errors.As(e, &ierr) {
if err2 := ierr.Unwrap(); err2 != nil {
// TODO: display stacktrace with xerrors
Expand Down
6 changes: 3 additions & 3 deletions internal/app/private.go
Expand Up @@ -11,10 +11,10 @@ import (
"github.com/reearth/reearth-backend/internal/graphql"
"github.com/reearth/reearth-backend/internal/usecase"
"github.com/reearth/reearth-backend/internal/usecase/repo"
err1 "github.com/reearth/reearth-backend/pkg/error"
"github.com/reearth/reearth-backend/pkg/id"
"github.com/reearth/reearth-backend/pkg/layer/encoding"
"github.com/reearth/reearth-backend/pkg/layer/merging"
"github.com/reearth/reearth-backend/pkg/rerror"
"github.com/reearth/reearth-backend/pkg/user"
)

Expand Down Expand Up @@ -80,14 +80,14 @@ func privateAPI(
}
scenes, err := repos.Scene.FindIDsByTeam(ctx, op.ReadableTeams)
if err != nil {
if errors.Is(err1.ErrNotFound, err) {
if errors.Is(rerror.ErrNotFound, err) {
return &echo.HTTPError{Code: http.StatusNotFound, Message: err}
}
return &echo.HTTPError{Code: http.StatusInternalServerError, Message: err}
}
layer, err := repos.Layer.FindByID(ctx, lid, scenes)
if err != nil {
if errors.Is(err1.ErrNotFound, err) {
if errors.Is(rerror.ErrNotFound, err) {
return &echo.HTTPError{Code: http.StatusNotFound, Message: err}
}
return &echo.HTTPError{Code: http.StatusInternalServerError, Message: err}
Expand Down
16 changes: 8 additions & 8 deletions internal/app/published_test.go
Expand Up @@ -12,7 +12,7 @@ import (

"github.com/labstack/echo/v4"
"github.com/reearth/reearth-backend/internal/usecase/interfaces"
err1 "github.com/reearth/reearth-backend/pkg/error"
"github.com/reearth/reearth-backend/pkg/rerror"
"github.com/stretchr/testify/assert"
)

Expand All @@ -31,7 +31,7 @@ func TestPublishedAuthMiddleware(t *testing.T) {
BasicAuthPassword: "baar",
}, nil
}
return interfaces.ProjectPublishedMetadata{}, err1.ErrNotFound
return interfaces.ProjectPublishedMetadata{}, rerror.ErrNotFound
})(func(c echo.Context) error {
return c.String(http.StatusOK, "test")
})
Expand Down Expand Up @@ -107,7 +107,7 @@ func TestPublishedData(t *testing.T) {
if name == "prj" {
return strings.NewReader("aaa"), nil
}
return nil, err1.ErrNotFound
return nil, rerror.ErrNotFound
})

testCases := []struct {
Expand All @@ -117,12 +117,12 @@ func TestPublishedData(t *testing.T) {
}{
{
Name: "empty",
Error: err1.ErrNotFound,
Error: rerror.ErrNotFound,
},
{
Name: "not found",
PublishedName: "pr",
Error: err1.ErrNotFound,
Error: rerror.ErrNotFound,
},
{
Name: "ok",
Expand Down Expand Up @@ -164,7 +164,7 @@ func TestPublishedIndex(t *testing.T) {
}{
{
Name: "empty",
Error: err1.ErrNotFound,
Error: rerror.ErrNotFound,
},
{
Name: "empty index",
Expand All @@ -174,7 +174,7 @@ func TestPublishedIndex(t *testing.T) {
{
Name: "not found",
PublishedName: "pr",
Error: err1.ErrNotFound,
Error: rerror.ErrNotFound,
},
{
Name: "ok",
Expand Down Expand Up @@ -203,7 +203,7 @@ func TestPublishedIndex(t *testing.T) {
assert.Equal("http://example.com/aaa/bbb", url.String())
return "index", nil
}
return "", err1.ErrNotFound
return "", rerror.ErrNotFound
})(c)

if tc.Error == nil {
Expand Down
4 changes: 2 additions & 2 deletions internal/graphql/resolver_project.go
Expand Up @@ -5,8 +5,8 @@ import (

graphql1 "github.com/reearth/reearth-backend/internal/adapter/graphql"
"github.com/reearth/reearth-backend/internal/graphql/dataloader"
err1 "github.com/reearth/reearth-backend/pkg/error"
"github.com/reearth/reearth-backend/pkg/id"
"github.com/reearth/reearth-backend/pkg/rerror"
)

func (r *Resolver) Project() ProjectResolver {
Expand All @@ -27,7 +27,7 @@ func (r *projectResolver) Scene(ctx context.Context, obj *graphql1.Project) (*gr
defer exit()

s, err := r.config.Controllers.SceneController.FindByProject(ctx, id.ProjectID(obj.ID), getOperator(ctx))
if err != nil && err != err1.ErrNotFound {
if err != nil && err != rerror.ErrNotFound {
return nil, err
}
return s, nil
Expand Down
6 changes: 3 additions & 3 deletions internal/graphql/resolver_property.go
Expand Up @@ -6,8 +6,8 @@ import (

graphql1 "github.com/reearth/reearth-backend/internal/adapter/graphql"
"github.com/reearth/reearth-backend/internal/graphql/dataloader"
err1 "github.com/reearth/reearth-backend/pkg/error"
"github.com/reearth/reearth-backend/pkg/id"
"github.com/reearth/reearth-backend/pkg/rerror"
)

func (r *Resolver) Property() PropertyResolver {
Expand Down Expand Up @@ -72,7 +72,7 @@ func (r *propertyResolver) Layer(ctx context.Context, obj *graphql1.Property) (g
defer exit()

l, err := r.config.Controllers.LayerController.FetchByProperty(ctx, id.PropertyID(obj.ID), getOperator(ctx))
if err != nil || errors.Is(err, err1.ErrNotFound) {
if err != nil || errors.Is(err, rerror.ErrNotFound) {
return nil, nil
}
return l, err
Expand All @@ -84,7 +84,7 @@ func (r *propertyResolver) Merged(ctx context.Context, obj *graphql1.Property) (

l, err := r.config.Controllers.LayerController.FetchByProperty(ctx, id.PropertyID(obj.ID), getOperator(ctx))
if err != nil {
if errors.Is(err, err1.ErrNotFound) {
if errors.Is(err, rerror.ErrNotFound) {
return nil, nil
}
return nil, err
Expand Down
8 changes: 4 additions & 4 deletions internal/infrastructure/adapter/plugin.go
Expand Up @@ -5,9 +5,9 @@ import (
"errors"

"github.com/reearth/reearth-backend/internal/usecase/repo"
err1 "github.com/reearth/reearth-backend/pkg/error"
"github.com/reearth/reearth-backend/pkg/id"
"github.com/reearth/reearth-backend/pkg/plugin"
"github.com/reearth/reearth-backend/pkg/rerror"
)

// TODO: ここで幅優先探索していくアルゴリズムを書いてmongoからビルトインの検索ロジックを除去する
Expand All @@ -27,7 +27,7 @@ func NewPlugin(readers []repo.Plugin, writer repo.Plugin) repo.Plugin {
func (r *pluginRepo) FindByID(ctx context.Context, id id.PluginID) (*plugin.Plugin, error) {
for _, re := range r.readers {
if res, err := re.FindByID(ctx, id); err != nil {
if errors.Is(err, err1.ErrNotFound) {
if errors.Is(err, rerror.ErrNotFound) {
continue
} else {
return nil, err
Expand All @@ -36,14 +36,14 @@ func (r *pluginRepo) FindByID(ctx context.Context, id id.PluginID) (*plugin.Plug
return res, nil
}
}
return nil, err1.ErrNotFound
return nil, rerror.ErrNotFound
}

func (r *pluginRepo) FindByIDs(ctx context.Context, ids []id.PluginID) ([]*plugin.Plugin, error) {
results := make([]*plugin.Plugin, 0, len(ids))
for _, id := range ids {
res, err := r.FindByID(ctx, id)
if err != nil && err != err1.ErrNotFound {
if err != nil && err != rerror.ErrNotFound {
return nil, err
}
results = append(results, res)
Expand Down
12 changes: 6 additions & 6 deletions internal/infrastructure/adapter/property_schema.go
Expand Up @@ -5,9 +5,9 @@ import (
"errors"

"github.com/reearth/reearth-backend/internal/usecase/repo"
err1 "github.com/reearth/reearth-backend/pkg/error"
"github.com/reearth/reearth-backend/pkg/id"
"github.com/reearth/reearth-backend/pkg/property"
"github.com/reearth/reearth-backend/pkg/rerror"
)

// TODO: ここで幅優先探索していくアルゴリズムを書いてmongoからビルトインの検索ロジックを除去する
Expand All @@ -27,7 +27,7 @@ func NewPropertySchema(readers []repo.PropertySchema, writer repo.PropertySchema
func (r *propertySchema) FindByID(ctx context.Context, id id.PropertySchemaID) (*property.Schema, error) {
for _, re := range r.readers {
if res, err := re.FindByID(ctx, id); err != nil {
if errors.Is(err, err1.ErrNotFound) {
if errors.Is(err, rerror.ErrNotFound) {
continue
} else {
return nil, err
Expand All @@ -36,14 +36,14 @@ func (r *propertySchema) FindByID(ctx context.Context, id id.PropertySchemaID) (
return res, nil
}
}
return nil, err1.ErrNotFound
return nil, rerror.ErrNotFound
}

func (r *propertySchema) FindByIDs(ctx context.Context, ids []id.PropertySchemaID) (property.SchemaList, error) {
results := make(property.SchemaList, 0, len(ids))
for _, id := range ids {
res, err := r.FindByID(ctx, id)
if err != nil && err != err1.ErrNotFound {
if err != nil && err != rerror.ErrNotFound {
return nil, err
}
results = append(results, res)
Expand All @@ -53,14 +53,14 @@ func (r *propertySchema) FindByIDs(ctx context.Context, ids []id.PropertySchemaI

func (r *propertySchema) Save(ctx context.Context, p *property.Schema) error {
if r.writer == nil {
return err1.ErrInternalBy(errors.New("writer is not set"))
return rerror.ErrInternalBy(errors.New("writer is not set"))
}
return r.writer.Save(ctx, p)
}

func (r *propertySchema) SaveAll(ctx context.Context, p property.SchemaList) error {
if r.writer == nil {
return err1.ErrInternalBy(errors.New("writer is not set"))
return rerror.ErrInternalBy(errors.New("writer is not set"))
}
return r.writer.SaveAll(ctx, p)
}
12 changes: 6 additions & 6 deletions internal/infrastructure/fs/archive.go
Expand Up @@ -5,8 +5,8 @@ import (
"path"
"strings"

err1 "github.com/reearth/reearth-backend/pkg/error"
"github.com/reearth/reearth-backend/pkg/file"
"github.com/reearth/reearth-backend/pkg/rerror"
)

type archive struct {
Expand All @@ -24,9 +24,9 @@ func NewArchive(p string) (file.Archive, error) {
files, size, err := dirwalk(bp, "", 0)
if err != nil {
if os.IsNotExist(err) {
return nil, err1.ErrNotFound
return nil, rerror.ErrNotFound
}
return nil, err1.ErrInternalBy(err)
return nil, rerror.ErrInternalBy(err)
}
return &archive{
p: bp,
Expand All @@ -46,12 +46,12 @@ func (a *archive) Next() (f *file.File, derr error) {
a.counter++
fi, err := os.Open(path.Join(a.p, next))
if err != nil {
derr = err1.ErrInternalBy(err)
derr = rerror.ErrInternalBy(err)
return
}
stat, err := fi.Stat()
if err != nil {
derr = err1.ErrInternalBy(err)
derr = rerror.ErrInternalBy(err)
return
}

Expand All @@ -68,7 +68,7 @@ func (a *archive) Next() (f *file.File, derr error) {
func (a *archive) Close() error {
if a.fi != nil {
if err := a.fi.Close(); err != nil {
return err1.ErrInternalBy(err)
return rerror.ErrInternalBy(err)
}
a.fi = nil
}
Expand Down

0 comments on commit a3f8b6d

Please sign in to comment.