Skip to content

Commit

Permalink
chore: drop-off v0.1 error handling until rfc safe/must decision (#52)
Browse files Browse the repository at this point in the history
## Description
Due to the presence of [RFC: Error Handling
Safe/Must](https://github.com/orgs/go-sprout/discussions/32), the old
system of error described in
#5 can be drop off to prevent
usage of mechanism how will never used.

## Changes
- Remove `ErrHandling` options
- Remove `ErrChannel` options

## Fixes #51 

## Checklist
- [x] I have read the **CONTRIBUTING.md** document.
- [x] My code follows the code style of this project.
- [x] I have added tests to cover my changes.
- [x] All new and existing tests passed.
- [x] I have updated the documentation accordingly.
- [x] This change requires a change to the documentation on the website.

## Additional Information
The documentation are updated in this PR.
  • Loading branch information
42atomys authored Aug 18, 2024
1 parent 7aa208f commit 3b92902
Show file tree
Hide file tree
Showing 7 changed files with 9 additions and 90 deletions.
6 changes: 0 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,6 @@ handler := sprout.New(
// standard slog package or any other logger that implements the slog.Logger interface.
// By default, Sprout uses a slog.TextHandler.
sprout.WithLogger(slogLogger),
// Set the error handling behavior for the handler. By default, Sprout returns the default value of the return type without crashes or panics.
sprout.WithErrHandling(sprout.ErrHandlingReturnDefaultValue),
// Set the error channel for the handler. By default, Sprout does not use an error channel. If you set an error channel, Sprout will send errors to it.
// This options is only used when the error handling behavior is set to
// `ErrHandlingErrorChannel`
sprout.WithErrorChannel(errChan),
// Set the alias for a function. By default, Sprout use alias for some functions for backward compatibility with Sprig.
sprout.WithAlias("hello", "hi"),
)
Expand Down
9 changes: 0 additions & 9 deletions docs/introduction/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,6 @@ Sprout supports various customization options using handler options:
logger := slog.New(slog.NewTextHandler(os.Stdout))
handler := sprout.New(sprout.WithLogger(logger))
```
* **Error Handling:**

You can specify how errors are handled within the handler:

```go
handler := sprout.New(sprout.WithErrHandling(sprout.ErrHandlingPanic))
```

Available error handling strategies include returning default values, panicking, or sending errors to a channel.
* **Aliases Management:**\
You can specify your custom aliases directly on your handler:

Expand Down
6 changes: 2 additions & 4 deletions docs/migration-from-sprig.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,10 @@ Nothing to do, using the new handler is enough.
### 3. **Error Handling**

* **Sprig:** Limited and inconsistent error handling, with some functions causing panics (see [#panicking-functions](migration-from-sprig.md#panicking-functions "mention")), and not fully adhering to Go template standards.
* **Sprout:** Offers configurable error handling strategies, including returning default values, triggering panics, or using error channels, providing a more consistent and flexible approach.
* **Sprout:** Offers configurable error handling strategies, including returning default values, or return error to stop template generation, providing a more consistent and flexible approach.

{% hint style="info" %}
**Migration Tip**

Set your preferred error handling strategy using `WithErrHandling` when creating your handler.
An RFC is currently open for feedback and discussion. You can view and participate in the RFC [here](https://github.com/orgs/go-sprout/discussions/32).
{% endhint %}

### 4. **Function Aliases**
Expand Down
31 changes: 0 additions & 31 deletions error.go

This file was deleted.

2 changes: 0 additions & 2 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ type Handler interface {
// DefaultHandler manages function execution with configurable error handling
// and logging.
type DefaultHandler struct {
ErrHandling ErrHandling
errChan chan error
logger *slog.Logger
registries []Registry
cachedFuncsMap FunctionMap
Expand Down
6 changes: 2 additions & 4 deletions sprout.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,8 @@ type HandlerOption[T Handler] func(T)
// a specific registry.
func New(opts ...HandlerOption[*DefaultHandler]) *DefaultHandler {
dh := &DefaultHandler{
ErrHandling: ErrHandlingReturnDefaultValue,
errChan: make(chan error),
logger: slog.New(slog.NewTextHandler(os.Stdout, nil)),
registries: make([]Registry, 0),
logger: slog.New(slog.NewTextHandler(os.Stdout, nil)),
registries: make([]Registry, 0),

cachedFuncsMap: make(FunctionMap),
cachedFuncsAlias: make(FunctionAliasMap),
Expand Down
39 changes: 5 additions & 34 deletions sprout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package sprout

import (
"log/slog"
"os"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -11,37 +12,21 @@ func TestNewFunctionHandler_DefaultValues(t *testing.T) {
handler := NewFunctionHandler()

assert.NotNil(t, handler)
assert.Equal(t, ErrHandlingReturnDefaultValue, handler.ErrHandling)
assert.NotNil(t, handler.errChan)
assert.NotNil(t, handler.Logger)
}

func TestNewFunctionHandler_CustomValues(t *testing.T) {
errChan := make(chan error, 1)
logger := slog.New(&slog.TextHandler{})
logger := slog.New(slog.NewTextHandler(os.Stdout, nil))
handler := NewFunctionHandler(
WithErrHandling(ErrHandlingPanic),
WithLogger(logger),
WithErrorChannel(errChan),
)

assert.NotNil(t, handler)
assert.Equal(t, ErrHandlingPanic, handler.ErrHandling)
assert.Equal(t, errChan, handler.errChan)
assert.Equal(t, logger, handler.Logger())
}

func TestWithErrHandling(t *testing.T) {
option := WithErrHandling(ErrHandlingPanic)

handler := NewFunctionHandler()
option(handler) // Apply the option

assert.Equal(t, ErrHandlingPanic, handler.ErrHandling)
}

func TestWithLogger(t *testing.T) {
logger := slog.New(&slog.TextHandler{})
logger := slog.New(slog.NewTextHandler(os.Stdout, nil))
option := WithLogger(logger)

handler := NewFunctionHandler()
Expand All @@ -50,21 +35,9 @@ func TestWithLogger(t *testing.T) {
assert.Equal(t, logger, handler.Logger())
}

func TestWithErrorChannel(t *testing.T) {
errChan := make(chan error, 1)
option := WithErrorChannel(errChan)

handler := NewFunctionHandler()
option(handler) // Apply the option

assert.Equal(t, errChan, handler.errChan)
}

func TestWithParser(t *testing.T) {
fnHandler := &DefaultHandler{
ErrHandling: ErrHandlingErrorChannel,
logger: slog.New(&slog.TextHandler{}),
errChan: make(chan error, 1),
logger: slog.New(slog.NewTextHandler(os.Stdout, nil)),
}
option := WithHandler(fnHandler)

Expand All @@ -76,9 +49,7 @@ func TestWithParser(t *testing.T) {

func TestWithNilHandler(t *testing.T) {
fnHandler := &DefaultHandler{
ErrHandling: ErrHandlingErrorChannel,
logger: slog.New(&slog.TextHandler{}),
errChan: make(chan error, 1),
logger: slog.New(slog.NewTextHandler(os.Stdout, nil)),
}
option := WithHandler(nil)

Expand Down

0 comments on commit 3b92902

Please sign in to comment.