/*
 * SPDX-FileCopyrightText: © 2017-2025 Istari Digital, Inc.
 * SPDX-License-Identifier: Apache-2.0
 */

package y

// This file contains some functions for error handling. Note that we are moving
// towards using x.Trace, i.e., rpc tracing using net/tracer. But for now, these
// functions are useful for simple checks logged on one machine.
// Some common use cases are:
// (1) You receive an error from external lib, and would like to check/log fatal.
//     For this, use x.Check, x.Checkf. These will check for err != nil, which is
//     more common in Go. If you want to check for boolean being true, use
//		   x.Assert, x.Assertf.
// (2) You receive an error from external lib, and would like to pass on with some
//     stack trace information. In this case, use x.Wrap or x.Wrapf.
// (3) You want to generate a new error with stack trace info. Use x.Errorf.

import (
	
	
	
)

var debugMode = false

// Check logs fatal if err != nil.
func ( error) {
	if  != nil {
		log.Fatalf("%+v", Wrap(, ""))
	}
}

// Check2 acts as convenience wrapper around Check, using the 2nd argument as error.
func ( interface{},  error) {
	Check()
}

// AssertTrue asserts that b is true. Otherwise, it would log fatal.
func ( bool) {
	if ! {
		log.Fatalf("%+v", errors.New("Assert failed"))
	}
}

// AssertTruef is AssertTrue with extra info.
func ( bool,  string,  ...interface{}) {
	if ! {
		log.Fatalf("%+v", fmt.Errorf(, ...))
	}
}

// Wrap wraps errors from external lib.
func ( error,  string) error {
	if !debugMode {
		if  == nil {
			return nil
		}
		return fmt.Errorf("%s err: %+v", , )
	}
	return fmt.Errorf("%s: %w", , )
}

// Wrapf is Wrap with extra info.
func ( error,  string,  ...interface{}) error {
	return Wrap(, fmt.Sprintf(, ...))
}

func (,  error) error {
	if  != nil &&  != nil {
		return fmt.Errorf("%v; %v", , )
	}
	if  != nil &&  == nil {
		return fmt.Errorf("%v", )
	}
	if  == nil &&  != nil {
		return fmt.Errorf("%v", )
	}
	return nil
}