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

feat(p/ufmt): add ufmt.Errorf #1767

Merged
merged 2 commits into from
Mar 13, 2024
Merged

Conversation

notJoon
Copy link
Member

@notJoon notJoon commented Mar 13, 2024

Description

Added the Errorf function to ufmt based on ufmt.Sprintf.

Previously, formatting errors involved a method like using errors.New(ufmt.Sprintf(XXX)), but this approach is not only cumbersome but also, as using Errorf is recommended in Go, I have newly incorporated it.

Usage

Simple Error

This example demonstrates the process of using Errorf to format and print error messages.

package main

import (
	"gno.land/p/demo/ufmt"
)

func foo() error {
	return fmt.Errorf("an error occurred in simpleFunction")
}

func main() {
	if err := foo(); err != nil {
             println("Error:", err)
	}
}

With Panic

Using panic is the recommended practice in effective gno, In this example, format the error and then output the error message with panic.

package main

import (
	"gno.land/p/demo/ufmt"
)


func foo() {
	msg := ufmt.Errorf("error: %s", "something went wrong")
	panic(msg)
}

@notJoon notJoon requested review from a team as code owners March 13, 2024 14:04
@github-actions github-actions bot added the 🧾 package/realm Tag used for new Realms or Packages. label Mar 13, 2024
Copy link
Contributor

@leohhhn leohhhn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Thanks 🙏

Please just fix the red CI :)

@thehowl thehowl changed the title feat(gno.land): add ufmt.Errorf feat(p/ufmt): add ufmt.Errorf Mar 13, 2024
examples/gno.land/p/demo/ufmt/ufmt.gno Outdated Show resolved Hide resolved
examples/gno.land/p/demo/ufmt/ufmt_test.gno Outdated Show resolved Hide resolved
examples/gno.land/p/demo/ufmt/ufmt_test.gno Outdated Show resolved Hide resolved
@thehowl thehowl merged commit 3481a03 into gnolang:master Mar 13, 2024
69 of 72 checks passed
@notJoon notJoon deleted the ufmt-add-errorf branch March 14, 2024 03:11
thehowl pushed a commit that referenced this pull request Mar 14, 2024
…rorf` (#1772)

## Description

Replace previouse formatted error creations with `fmt.Errof` (in gno,
`ufmt.Errorf`) to simplify error creation and formatting.

Follow up for PR #1767
albttx pushed a commit to albttx/gno that referenced this pull request Mar 15, 2024
# Description

Added the `Errorf` function to `ufmt` based on `ufmt.Sprintf`. 

Previously, formatting errors involved a method like using
`errors.New(ufmt.Sprintf(XXX))`, but this approach is not only
cumbersome but also, as using `Errorf` is recommended in Go, I have
newly incorporated it.

## Usage

### Simple Error

This example demonstrates the process of using `Errorf` to format and
print error messages.

```go
package main

import (
	"gno.land/p/demo/ufmt"
)

func foo() error {
	return fmt.Errorf("an error occurred in simpleFunction")
}

func main() {
	if err := foo(); err != nil {
             println("Error:", err)
	}
}
```

### With Panic

Using panic is the recommended practice in [effective
gno](<https://docs.gno.land/concepts/effective-gno/#embrace-panic>), In
this example, format the error and then output the error message with
`panic`.

```go
package main

import (
	"gno.land/p/demo/ufmt"
)


func foo() {
	msg := ufmt.Errorf("error: %s", "something went wrong")
	panic(msg)
}
```

---------

Co-authored-by: Morgan <git@howl.moe>
albttx pushed a commit to albttx/gno that referenced this pull request Mar 15, 2024
…rorf` (gnolang#1772)

## Description

Replace previouse formatted error creations with `fmt.Errof` (in gno,
`ufmt.Errorf`) to simplify error creation and formatting.

Follow up for PR gnolang#1767
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🧾 package/realm Tag used for new Realms or Packages.
Projects
Status: Done
Archived in project
Development

Successfully merging this pull request may close these issues.

None yet

3 participants