github.com/mdaxf/iac@v0.0.0-20240519030858-58a061660378/engine/function/throwErrorFuncs.go (about)

     1  package funcs
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  )
     7  
     8  type ThrowErrorFuncs struct {
     9  }
    10  
    11  // Execute executes the ThrowErrorFuncs function.
    12  // It measures the execution time and logs the performance.
    13  // It recovers from any panics and logs the error.
    14  // It rolls back the database transaction, cancels the execution context, and sets the error message.
    15  func (cf *ThrowErrorFuncs) Execute(f *Funcs) {
    16  	// function execution start time
    17  	startTime := time.Now()
    18  	defer func() {
    19  		// calculate elapsed time
    20  		elapsed := time.Since(startTime)
    21  		// log performance with duration
    22  		f.iLog.PerformanceWithDuration("engine.funcs.ThrowErrorFuncs.Execute", elapsed)
    23  	}()
    24  
    25  	defer func() {
    26  		// recover from any panics
    27  		if err := recover(); err != nil {
    28  			// log the error
    29  			f.iLog.Error(fmt.Sprintf("There is error to engine.funcs.ThrowErrorFuncs.Execute with error: %s", err))
    30  			// cancel execution and set error message
    31  			f.CancelExecution(fmt.Sprintf("There is error to engine.funcs.ThrowErrorFuncs.Execute with error: %s", err))
    32  			f.ErrorMessage = fmt.Sprintf("There is error to engine.funcs.ThrowErrorFuncs.Execute with error: %s", err)
    33  		}
    34  	}()
    35  
    36  	// log debug information
    37  	f.iLog.Debug(fmt.Sprintf("ThrowErrorFuncs Execute: %v", f))
    38  	// rollback database transaction
    39  	f.DBTx.Rollback()
    40  	// cancel execution context
    41  	f.CtxCancel()
    42  	// mark execution as done
    43  	f.Ctx.Done()
    44  	// set error message
    45  	f.ErrorMessage = "ThrowErrorFuncs Execute"
    46  }