1
0
mirror of https://github.com/IBM/fp-go.git synced 2025-11-23 22:14:53 +02:00
Files
fp-go/v2/errors/messages.go
Carsten Leue 3385c705dc Implement v2 using type aliases (#141)
* fix: initial checkin of v2

Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>

* fix: slowly migrate IO

Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>

* fix: migrate MonadTraverseArray and TraverseArray

Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>

* fix: migrate traversal

Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>

* fix: complete migration of IO

Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>

* fix: migrate ioeither

Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>

* fix: refactorY

Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>

* fix: next step in migration

Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>

* fix: adjust IO generation code

Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>

* fix: get rid of more IO methods

Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>

* fix: get rid of more IO

* fix: convert iooption

Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>

* fix: convert reader

Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>

* fix: convert a bit of reader

Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>

* fix: new build script

Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>

* fix: cleanup

Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>

* fix: reformat

Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>

* fix: simplify

Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>

* fix: some cleanup

Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>

* fix: adjust Pair to Haskell semantic

Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>

* fix: documentation and testcases

Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>

* fix: some performance optimizations

Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>

* fix: remove coverage

Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>

* fix: better doc

Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>

---------

Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>
2025-11-06 09:27:00 +01:00

104 lines
3.7 KiB
Go

// Copyright (c) 2023 - 2025 IBM Corp.
// All rights reserved.
//
// 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 errors
import (
"fmt"
A "github.com/IBM/fp-go/v2/array"
"github.com/IBM/fp-go/v2/endomorphism"
)
// OnNone generates a nullary function that produces a formatted error.
// This is useful when you need to create an error lazily, such as when
// handling the None case in an Option type.
//
// Example:
//
// getError := OnNone("value not found")
// err := getError() // returns error: "value not found"
//
// getErrorWithArgs := OnNone("failed to load %s", "config.json")
// err2 := getErrorWithArgs() // returns error: "failed to load config.json"
func OnNone(msg string, args ...any) func() error {
return func() error {
return fmt.Errorf(msg, args...)
}
}
// OnSome generates a unary function that produces a formatted error.
// The function takes a value of type T and includes it in the error message.
// If no additional args are provided, the value is used as the only format argument.
// If additional args are provided, the value becomes the first format argument.
//
// This is useful when you need to create an error that includes information
// about a value, such as when handling the Some case in an Option type.
//
// Example:
//
// // Without additional args - value is the only format argument
// makeError := OnSome[int]("invalid value: %d")
// err := makeError(42) // returns error: "invalid value: 42"
//
// // With additional args - value is the first format argument
// makeError2 := OnSome[string]("failed to process %s in file %s", "data.txt")
// err2 := makeError2("record123") // returns error: "failed to process record123 in file data.txt"
func OnSome[T any](msg string, args ...any) func(T) error {
l := len(args)
if l == 0 {
return func(value T) error {
return fmt.Errorf(msg, value)
}
}
return func(value T) error {
data := make([]any, l+1)
data[0] = value
copy(data[1:], args)
return fmt.Errorf(msg, data...)
}
}
// OnError generates a unary function that produces a formatted error with error wrapping.
// The argument to that function is the root cause of the error and the message will be
// augmented with a format string containing %w for proper error wrapping.
//
// This is useful for adding context to errors while preserving the error chain,
// allowing errors.Is and errors.As to work correctly.
//
// Example:
//
// wrapError := OnError("failed to load configuration from %s", "config.json")
// rootErr := errors.New("file not found")
// wrappedErr := wrapError(rootErr)
// // returns error: "failed to load configuration from config.json, Caused By: file not found"
// // errors.Is(wrappedErr, rootErr) returns true
func OnError(msg string, args ...any) endomorphism.Endomorphism[error] {
return func(err error) error {
return fmt.Errorf(msg+", Caused By: %w", A.ArrayConcatAll(args, A.Of[any](err))...)
}
}
// ToString converts an error to its string representation by calling the Error() method.
// This is useful in functional pipelines where you need to transform an error into a string.
//
// Example:
//
// err := errors.New("something went wrong")
// msg := ToString(err) // returns "something went wrong"
func ToString(err error) string {
return err.Error()
}