github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/engine/framework/internal/errors/fail_fast.go (about)

     1  // Copyright 2022 PingCAP, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language
    12  
    13  package errors
    14  
    15  import (
    16  	"github.com/pingcap/tiflow/pkg/errors"
    17  )
    18  
    19  // FailFastError is used internally in the framework to
    20  // mark the error has needing to fail the worker or master immediately.
    21  // FailFastError implements Cause() so that it is compatible with
    22  // `errors` and `pingcap/errors`.
    23  type FailFastError struct {
    24  	cause error
    25  }
    26  
    27  // FailFast wraps an error and marks it as needing to fail
    28  // the worker or master immediately.
    29  func FailFast(errIn error) error {
    30  	if errIn == nil {
    31  		return nil
    32  	}
    33  	return &FailFastError{cause: errIn}
    34  }
    35  
    36  // Cause implement causer for the error.
    37  func (e *FailFastError) Cause() error {
    38  	return e.cause
    39  }
    40  
    41  // Unwrap returns cause of the error.
    42  // It allows Error to work with errors.Is() and errors.As() from the Go
    43  // standard package.
    44  func (e *FailFastError) Unwrap() error {
    45  	return e.cause
    46  }
    47  
    48  func (e *FailFastError) Error() string {
    49  	return e.cause.Error()
    50  }
    51  
    52  // IsFailFastError tells whether the error is FailFastError.
    53  func IsFailFastError(errIn error) bool {
    54  	var out *FailFastError
    55  
    56  	// We use `As` instead of `Is` because
    57  	// `Is` requires exact equalities instead
    58  	// of type matching.
    59  	return errors.As(errIn, &out)
    60  }