go.temporal.io/server@v1.23.0/common/failure/failure.go (about)

     1  // The MIT License
     2  //
     3  // Copyright (c) 2020 Temporal Technologies Inc.  All rights reserved.
     4  //
     5  // Copyright (c) 2020 Uber Technologies, Inc.
     6  //
     7  // Permission is hereby granted, free of charge, to any person obtaining a copy
     8  // of this software and associated documentation files (the "Software"), to deal
     9  // in the Software without restriction, including without limitation the rights
    10  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    11  // copies of the Software, and to permit persons to whom the Software is
    12  // furnished to do so, subject to the following conditions:
    13  //
    14  // The above copyright notice and this permission notice shall be included in
    15  // all copies or substantial portions of the Software.
    16  //
    17  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    18  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    19  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    20  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    21  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    22  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    23  // THE SOFTWARE.
    24  
    25  package failure
    26  
    27  import (
    28  	commonpb "go.temporal.io/api/common/v1"
    29  	enumspb "go.temporal.io/api/enums/v1"
    30  	failurepb "go.temporal.io/api/failure/v1"
    31  )
    32  
    33  const (
    34  	failureSourceServer = "Server"
    35  )
    36  
    37  func NewServerFailure(message string, nonRetryable bool) *failurepb.Failure {
    38  	f := &failurepb.Failure{
    39  		Message: message,
    40  		FailureInfo: &failurepb.Failure_ServerFailureInfo{ServerFailureInfo: &failurepb.ServerFailureInfo{
    41  			NonRetryable: nonRetryable,
    42  		}},
    43  	}
    44  
    45  	return f
    46  }
    47  
    48  func NewResetWorkflowFailure(message string, lastHeartbeatDetails *commonpb.Payloads) *failurepb.Failure {
    49  	f := &failurepb.Failure{
    50  		Message: message,
    51  		FailureInfo: &failurepb.Failure_ResetWorkflowFailureInfo{ResetWorkflowFailureInfo: &failurepb.ResetWorkflowFailureInfo{
    52  			LastHeartbeatDetails: lastHeartbeatDetails,
    53  		}},
    54  	}
    55  
    56  	return f
    57  }
    58  
    59  func NewTimeoutFailure(message string, timeoutType enumspb.TimeoutType) *failurepb.Failure {
    60  	f := &failurepb.Failure{
    61  		Message: message,
    62  		Source:  failureSourceServer,
    63  		FailureInfo: &failurepb.Failure_TimeoutFailureInfo{TimeoutFailureInfo: &failurepb.TimeoutFailureInfo{
    64  			TimeoutType: timeoutType,
    65  		}},
    66  	}
    67  
    68  	return f
    69  }
    70  
    71  func Truncate(f *failurepb.Failure, maxSize int) *failurepb.Failure {
    72  	if f == nil {
    73  		return nil
    74  	}
    75  
    76  	newFailure := &failurepb.Failure{
    77  		Source: f.Source,
    78  	}
    79  
    80  	// Keep failure info for ApplicationFailureInfo and for ServerFailureInfo to persist NonRetryable flag.
    81  	if f.GetApplicationFailureInfo() != nil {
    82  		newFailure.FailureInfo = &failurepb.Failure_ApplicationFailureInfo{ApplicationFailureInfo: &failurepb.ApplicationFailureInfo{
    83  			NonRetryable: f.GetApplicationFailureInfo().GetNonRetryable(),
    84  			Type:         f.GetApplicationFailureInfo().GetType(),
    85  		}}
    86  	}
    87  
    88  	if f.GetServerFailureInfo() != nil {
    89  		newFailure.FailureInfo = &failurepb.Failure_ServerFailureInfo{ServerFailureInfo: &failurepb.ServerFailureInfo{
    90  			NonRetryable: f.GetServerFailureInfo().GetNonRetryable(),
    91  		}}
    92  	}
    93  
    94  	if len(f.Message) > maxSize {
    95  		newFailure.Message = f.Message[:maxSize]
    96  		return newFailure
    97  	}
    98  	newFailure.Message = f.Message
    99  	maxSize -= len(newFailure.Message)
   100  
   101  	if len(f.StackTrace) > maxSize {
   102  		newFailure.StackTrace = f.StackTrace[:maxSize]
   103  		return newFailure
   104  	}
   105  	newFailure.StackTrace = f.StackTrace
   106  	maxSize -= len(newFailure.StackTrace)
   107  
   108  	newFailure.Cause = Truncate(f.Cause, maxSize)
   109  
   110  	return newFailure
   111  }