Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 0 additions & 22 deletions .gitattributes

This file was deleted.

10 changes: 8 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,14 @@ jobs:
test-project:
runs-on: ubuntu-20.04
steps:
- name: Setup
uses: actions/setup-go@v4
with:
go-version: "1.21"
- run: go version

- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Test
run: make test
run: make test
6 changes: 6 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
## ✒ 历史版本的特性介绍 (Features in old versions)

### v0.6.0-alpha

> 此版本发布于 2024-08-09

* 重构,调整使用姿势

### v0.5.2

> 此版本发布于 2023-07-27
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2022 FishGoddess
Copyright (c) 2024 FishGoddess

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the "Software"),
Expand Down
12 changes: 6 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
.PHONY: test fmt
.PHONY: fmt test

all: test fmt

test:
go test -cover ./...
all: fmt test

fmt:
go fmt ./...
go fmt ./...

test:
go test -cover -count=1 -test.cpu=1 ./...
51 changes: 14 additions & 37 deletions README.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![Go Doc](_icons/godoc.svg)](https://pkg.go.dev/github.com/FishGoddess/errors)
[![License](_icons/license.svg)](https://opensource.org/licenses/MIT)
[![License](_icons/coverage.svg)](_icons/coverage.svg)
[![Coverage](_icons/coverage.svg)](_icons/coverage.svg)
![Test](https://github.com/FishGoddess/errors/actions/workflows/test.yml/badge.svg)

**Errors** is a lib for handling error gracefully in Go.
Expand All @@ -25,50 +25,27 @@ import (
"github.com/FishGoddess/errors"
)

const (
codeTestError = 10000 // Your error's code
)
func main() {
// Use wrap function to create an *Error error which has code and message.
err := errors.Wrap(1000, "need login")
fmt.Println(err)

// TestError returns a test error.
func TestError(err error) error {
return errors.Wrap(err, codeTestError)
}
// You can get code and message of err anytime.
fmt.Println(err.Code(), err.Message())

// IsTestError if err is test error.
func IsTestError(err error) bool {
return errors.Is(err, codeTestError)
}
// Try these ways to get code and message!
// You will get default code or message if err doesn't have a code or message.
fmt.Println(errors.Code(err, 6699), errors.Message(err, "default message"))
fmt.Println(errors.Code(io.EOF, 6699), errors.Message(io.EOF, "default message"))

func main() {
// We provide three graceful ways to handle error in Go: Wrap() and Unwrap() and Is().
// Wrap wraps error with a code and Unwrap returns one error with this code.
// Is returns one error is with the same code or not.
// As you can see, we define two functions above, and this is the basic way to use this lib.
err := TestError(errors.New("something wrong"))
if IsTestError(err) {
fmt.Println("I got a test error")
}

// Also, we provide some basic errors for you:
err = errors.BadRequest(nil) // Classic enough! Ah :)
err = errors.Forbidden(nil) // Classic enough! Ah :)
err = errors.NotFound(nil) // Classic enough! Ah :)
err = errors.RequestTimeout(nil) // Classic enough! Ah :)
err = errors.InternalServerError(nil) // Classic enough! Ah :)
err = errors.DBError(nil)
err = errors.PageTokenInvalid(nil)

// Use WithMsg to carry a message.
err = errors.Wrap(io.EOF, codeTestError, errors.WithMsg("test"))
fmt.Println(err.Error())
fmt.Println(errors.Msg(err))
fmt.Println(errors.MsgOrDefault(io.EOF, "default error message"))
// Also, we provide some useful information carrier for you.
err = errors.Wrap(9999, "io timeout").With(io.EOF).WithCaller()
fmt.Println(err)
}

```

* [basic](_examples/basic.go)
* [status](_examples/status.go)

### 👥 Contributing

Expand Down
51 changes: 14 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![Go Doc](_icons/godoc.svg)](https://pkg.go.dev/github.com/FishGoddess/errors)
[![License](_icons/license.svg)](https://opensource.org/licenses/MIT)
[![License](_icons/coverage.svg)](_icons/coverage.svg)
[![Coverage](_icons/coverage.svg)](_icons/coverage.svg)
![Test](https://github.com/FishGoddess/errors/actions/workflows/test.yml/badge.svg)

**Errors** 是一个用于优雅地处理 Go 中错误的库。
Expand All @@ -25,50 +25,27 @@ import (
"github.com/FishGoddess/errors"
)

const (
codeTestError = 10000 // Your error's code
)
func main() {
// Use wrap function to create an *Error error which has code and message.
err := errors.Wrap(1000, "need login")
fmt.Println(err)

// TestError returns a test error.
func TestError(err error) error {
return errors.Wrap(err, codeTestError)
}
// You can get code and message of err anytime.
fmt.Println(err.Code(), err.Message())

// IsTestError if err is test error.
func IsTestError(err error) bool {
return errors.Is(err, codeTestError)
}
// Try these ways to get code and message!
// You will get default code or message if err doesn't have a code or message.
fmt.Println(errors.Code(err, 6699), errors.Message(err, "default message"))
fmt.Println(errors.Code(io.EOF, 6699), errors.Message(io.EOF, "default message"))

func main() {
// We provide three graceful ways to handle error in Go: Wrap() and Unwrap() and Is().
// Wrap wraps error with a code and Unwrap returns one error with this code.
// Is returns one error is with the same code or not.
// As you can see, we define two functions above, and this is the basic way to use this lib.
err := TestError(errors.New("something wrong"))
if IsTestError(err) {
fmt.Println("I got a test error")
}

// Also, we provide some basic errors for you:
err = errors.BadRequest(nil) // Classic enough! Ah :)
err = errors.Forbidden(nil) // Classic enough! Ah :)
err = errors.NotFound(nil) // Classic enough! Ah :)
err = errors.RequestTimeout(nil) // Classic enough! Ah :)
err = errors.InternalServerError(nil) // Classic enough! Ah :)
err = errors.DBError(nil)
err = errors.PageTokenInvalid(nil)

// Use WithMsg to carry a message.
err = errors.Wrap(io.EOF, codeTestError, errors.WithMsg("test"))
fmt.Println(err.Error())
fmt.Println(errors.Msg(err))
fmt.Println(errors.MsgOrDefault(io.EOF, "default error message"))
// Also, we provide some useful information carrier for you.
err = errors.Wrap(9999, "io timeout").With(io.EOF).WithCaller()
fmt.Println(err)
}

```

* [basic](_examples/basic.go)
* [status](_examples/status.go)

### 👥 贡献者

Expand Down
50 changes: 14 additions & 36 deletions _examples/basic.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2022 FishGoddess. All rights reserved.
// Copyright 2024 FishGoddess. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.

Expand All @@ -11,42 +11,20 @@ import (
"github.com/FishGoddess/errors"
)

const (
codeTestError = 10000 // Your error's code
)

// TestError returns a test error.
func TestError(err error) error {
return errors.Wrap(err, codeTestError)
}

// IsTestError if err is test error.
func IsTestError(err error) bool {
return errors.Is(err, codeTestError)
}

func main() {
// We provide three graceful ways to handle error in Go: Wrap() and Unwrap() and Is().
// Wrap wraps error with a code and Unwrap returns one error with this code.
// Is returns one error is with the same code or not.
// As you can see, we define two functions above, and this is the basic way to use this lib.
err := TestError(errors.New("something wrong"))
if IsTestError(err) {
fmt.Println("I got a test error")
}
// Use wrap function to create an *Error error which has code and message.
err := errors.Wrap(1000, "need login")
fmt.Println(err)

// You can get code and message of err anytime.
fmt.Println(err.Code(), err.Message())

// Also, we provide some basic errors for you:
err = errors.BadRequest(nil) // Classic enough! Ah :)
err = errors.Forbidden(nil) // Classic enough! Ah :)
err = errors.NotFound(nil) // Classic enough! Ah :)
err = errors.RequestTimeout(nil) // Classic enough! Ah :)
err = errors.InternalServerError(nil) // Classic enough! Ah :)
err = errors.DBError(nil)
err = errors.PageTokenInvalid(nil)
// Try these ways to get code and message!
// You will get default code or message if err doesn't have a code or message.
fmt.Println(errors.Code(err, 6699), errors.Message(err, "default message"))
fmt.Println(errors.Code(io.EOF, 6699), errors.Message(io.EOF, "default message"))

// Use WithMsg to carry a message.
err = errors.Wrap(io.EOF, codeTestError, errors.WithMsg("test"))
fmt.Println(err.Error())
fmt.Println(errors.Msg(err))
fmt.Println(errors.MsgOrDefault(io.EOF, "default error message"))
// Also, we provide some useful information carrier for you.
err = errors.Wrap(9999, "io timeout").With(io.EOF).WithCaller()
fmt.Println(err)
}
62 changes: 0 additions & 62 deletions _examples/status.go

This file was deleted.

4 changes: 2 additions & 2 deletions _icons/coverage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading