-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: more precise and detailed handling of errors #111
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ package paper | |
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/west2-online/fzuhelper-server/internal/paper/service" | ||
"github.com/west2-online/fzuhelper-server/kitex_gen/model" | ||
|
@@ -47,12 +48,12 @@ func (s *PaperServiceImpl) ListDirFiles(ctx context.Context, req *paper.ListDirF | |
|
||
success, fileDir, err = service.NewPaperService(ctx, s.ClientSet).GetDir(req) | ||
if !success { | ||
logger.Infof("Paper.ListDirFiles: get dir info failed from upyun") | ||
logger.Errorf("Paper.ListDirFiles: get dir info failed from upyun") | ||
resp.Base = base.BuildBaseResp(errors.New("failed to get info from upyun")) | ||
return resp, nil | ||
} | ||
if err != nil { | ||
logger.Infof("Paper.ListDirFiles: get dir info failed: %v", err) | ||
base.LogError(fmt.Errorf("Paper.ListDirFiles: get dir info failed: %w", err)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 将正常的err中的信息的日志集成进了 |
||
resp.Base = base.BuildBaseResp(errors.New("failed to get info from upyun")) | ||
return resp, nil | ||
} | ||
|
@@ -68,8 +69,7 @@ func (s *PaperServiceImpl) GetDownloadUrl(ctx context.Context, req *paper.GetDow | |
|
||
url, err := service.NewPaperService(ctx, s.ClientSet).GetDownloadUrl(req) | ||
if err != nil { | ||
logger.Infof("Paper.GetDownloadUrl: get download url failed: %v", err) | ||
resp.Base = base.BuildBaseResp(err) | ||
resp.Base = base.BuildRespAndLog(fmt.Errorf("Paper.GetDownloadUrl: get download url failed: %w", err)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这里是另一种日志输出err中信息的方法,他将日志与resp的build集成在了一起,减轻开发者心智负担 |
||
return resp, nil | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,6 +49,9 @@ func (s *PaperService) GetDir(req *paper.ListDirFilesRequest) (bool, *model.UpYu | |
return false, nil, fmt.Errorf("service.GetDir: get dir info failed: %w", err) | ||
} | ||
|
||
err = s.cache.Paper.SetFileDirCache(s.ctx, key, *fileDir) | ||
if err = s.cache.Paper.SetFileDirCache(s.ctx, key, *fileDir); err != nil { | ||
return true, fileDir, fmt.Errorf("service.GetDir: set file dir cache failed: %w", err) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这个地方并不产生错误,他只是错误的传递者之一,于是使用 |
||
} | ||
|
||
return true, fileDir, err | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
Copyright 2024 The west2-online Authors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package base | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/west2-online/fzuhelper-server/pkg/errno" | ||
) | ||
|
||
func TestBuildBaseResp(t *testing.T) { | ||
nilError := BuildBaseResp(nil) | ||
assert.Equal(t, int64(errno.SuccessCode), nilError.Code) | ||
assert.Equal(t, errno.Success.ErrorMsg, nilError.Msg) | ||
|
||
normalError := BuildBaseResp(fmt.Errorf("ok")) | ||
assert.Equal(t, int64(errno.InternalServiceErrorCode), normalError.Code) | ||
assert.Equal(t, "ok", normalError.Msg) | ||
|
||
errnoError := BuildBaseResp(errno.NewErrNo(200, "ok")) | ||
assert.Equal(t, int64(200), errnoError.Code) | ||
assert.Equal(t, "ok", errnoError.Msg) | ||
} | ||
|
||
func TestBuildSuccessResp(t *testing.T) { | ||
r := BuildSuccessResp() | ||
assert.Equal(t, int64(errno.SuccessCode), r.Code) | ||
assert.Equal(t, errno.Success.ErrorMsg, r.Msg) | ||
} | ||
|
||
func TestLogError(t *testing.T) { | ||
LogError(nil) | ||
LogError(fmt.Errorf("ok")) | ||
LogError(errno.Success) | ||
} | ||
|
||
func TestBuildRespAndLog(t *testing.T) { | ||
nilError := BuildBaseResp(nil) | ||
assert.Equal(t, int64(errno.SuccessCode), nilError.Code) | ||
assert.Equal(t, errno.Success.ErrorMsg, nilError.Msg) | ||
|
||
normalError := BuildBaseResp(fmt.Errorf("ok")) | ||
assert.Equal(t, int64(errno.InternalServiceErrorCode), normalError.Code) | ||
assert.Equal(t, "ok", normalError.Msg) | ||
|
||
errnoError := BuildBaseResp(errno.NewErrNo(200, "ok")) | ||
assert.Equal(t, int64(200), errnoError.Code) | ||
assert.Equal(t, "ok", errnoError.Msg) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,23 +18,23 @@ package paper | |
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/bytedance/sonic" | ||
|
||
"github.com/west2-online/fzuhelper-server/kitex_gen/model" | ||
"github.com/west2-online/fzuhelper-server/pkg/errno" | ||
) | ||
|
||
func (c *CachePaper) GetFileDirCache(ctx context.Context, key string) (bool, *model.UpYunFileDir, error) { | ||
ret := &model.UpYunFileDir{} | ||
|
||
data, err := c.client.Get(ctx, key).Bytes() | ||
if err != nil { | ||
return false, ret, fmt.Errorf("dal.GetFileDirCache: get dir info failed: %w", err) | ||
return false, ret, errno.Errorf(errno.InternalDatabaseErrorCode, "dal.GetFileDirCache: get dir info failed: %v", err) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这里的错误是origin error,也就是产生错误的地方,所以我们使用 |
||
} | ||
err = sonic.Unmarshal(data, &ret) | ||
if err != nil { | ||
return false, ret, fmt.Errorf("dal.GetFileDirCache: Unmarshal dir info failed: %w", err) | ||
return false, ret, errno.Errorf(errno.InternalJSONErrorCode, "dal.GetFileDirCache: Unmarshal dir info failed: %v", err) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 同上,产生错误的地方需要使用errno来包装一个栈信息 |
||
} | ||
return true, ret, nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,20 +18,23 @@ package paper | |
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/bytedance/sonic" | ||
|
||
"github.com/west2-online/fzuhelper-server/kitex_gen/model" | ||
"github.com/west2-online/fzuhelper-server/pkg/constants" | ||
"github.com/west2-online/fzuhelper-server/pkg/errno" | ||
) | ||
|
||
const TwoDay = 2 * constants.OneDay | ||
|
||
func (c *CachePaper) SetFileDirCache(ctx context.Context, key string, dir model.UpYunFileDir) error { | ||
data, err := sonic.Marshal(dir) | ||
if err != nil { | ||
return fmt.Errorf("dal.SetFileDirCache: Unmarshal dir info failed: %w", err) | ||
return errno.Errorf(errno.InternalJSONErrorCode, "dal.SetFileDirCache: Unmarshal dir info failed: %v", err) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 产生error的地方 |
||
} | ||
return c.client.Set(ctx, key, data, TwoDay).Err() | ||
if err = c.client.Set(ctx, key, data, TwoDay).Err(); err != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 产生 |
||
return errno.Errorf(errno.InternalDatabaseErrorCode, "%v", err) | ||
} | ||
return nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
因为原来的代码并没有对error的内容进行输出,所以这里也没有更改逻辑,只更改了日志等级