Skip to content
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

log critical raw error, and throw predefined error variable and traceid #7

Closed
cnlangzi opened this issue Aug 20, 2021 · 2 comments · Fixed by #8
Closed

log critical raw error, and throw predefined error variable and traceid #7

cnlangzi opened this issue Aug 20, 2021 · 2 comments · Fixed by #8
Assignees
Milestone

Comments

@cnlangzi
Copy link
Contributor

for a better user and debugging experience, we should have a better error handling

  • show friendly error message instead of raw error
  • log raw error ,and generate traceid for debugging. it is easier to find correct raw error in logging system, and trigger DevOps alert.

for example :


var ErrServiceUnavailable = errors.New("service unavailable")
if err != nil { //any network/db error
    if errors.Is(err, ErrHasNotShared) {
         return nil, err
    }
    return nil, errors.ThrowLog(ErrServiceUnavailable,err)
}

if shareInfo == nil ||  shareInfo.Removed {
    return nil, ErrHasNotShared
}


var ErrHasNotShared = errors.New("client does not have permission to download the file. share revoked")


func GetShareInfo(ctx context.Context, clientID string, filePathHash string) (*ShareInfo, error) {
	db := datastore.GetStore().GetTransaction(ctx)
	shareInfo := &ShareInfo{}
	err := db.Table(TableName()).
		Where(&ShareInfo{
			ClientID:     clientID,
			FilePathHash: filePathHash,
		}).
		First(shareInfo).Error

	if err != nil {

    if errors.Is(err, gorm.ErrRecordNotFound) {
        return nil, ErrHasNotShared
    }

		return nil, err
	}
	return shareInfo, nil
}


@NoSkillGuy
Copy link
Contributor

@cnlangzi What you said is correct, but should it be the responsibility of this package?

This package should give the actual error, it's up to the client who is using this package on how to give a friendly message and log it for debugging purpose right

@cnlangzi
Copy link
Contributor Author

cnlangzi commented Aug 24, 2021

@cnlangzi What you said is correct, but should it be the responsibility of this package?

This package should give the actual error, it's up to the client who is using this package on how to give a friendly message and log it for debugging purpose right

check code example on PR #8

@cnlangzi cnlangzi added this to the 1.0.3 milestone Aug 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants