// The MIT License
//
// Copyright (c) 2020 Temporal Technologies Inc.  All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

syntax = "proto3";

package temporal.api.failure.v1;

option go_package = "go.temporal.io/api/failure/v1;failure";
option java_package = "io.temporal.api.failure.v1";
option java_multiple_files = true;
option java_outer_classname = "MessageProto";
option ruby_package = "Temporalio::Api::Failure::V1";
option csharp_namespace = "Temporalio.Api.Failure.V1";

import "temporal/api/common/v1/message.proto";
import "temporal/api/enums/v1/workflow.proto";

import "google/protobuf/duration.proto";

message ApplicationFailureInfo {
    string type = 1;
    bool non_retryable = 2;
    temporal.api.common.v1.Payloads details = 3;
    // next_retry_delay can be used by the client to override the activity
    // retry interval calculated by the retry policy. Retry attempts will
    // still be subject to the maximum retries limit and total time limit
    // defined by the policy.
    // ATTENTION: this value will be ignored if set for failures produced by
    // the workflow.
    google.protobuf.Duration next_retry_delay = 4;
}

message TimeoutFailureInfo {
    temporal.api.enums.v1.TimeoutType timeout_type = 1;
    temporal.api.common.v1.Payloads last_heartbeat_details = 2;
}

message CanceledFailureInfo {
    temporal.api.common.v1.Payloads details = 1;
}

message TerminatedFailureInfo {
}

message ServerFailureInfo {
    bool non_retryable = 1;
}

message ResetWorkflowFailureInfo {
    temporal.api.common.v1.Payloads last_heartbeat_details = 1;
}

message ActivityFailureInfo {
    int64 scheduled_event_id = 1;
    int64 started_event_id = 2;
    string identity = 3;
    temporal.api.common.v1.ActivityType activity_type = 4;
    string activity_id = 5;
    temporal.api.enums.v1.RetryState retry_state = 6;
}

message ChildWorkflowExecutionFailureInfo {
    string namespace = 1;
    temporal.api.common.v1.WorkflowExecution workflow_execution = 2;
    temporal.api.common.v1.WorkflowType workflow_type = 3;
    int64 initiated_event_id = 4;
    int64 started_event_id = 5;
    temporal.api.enums.v1.RetryState retry_state = 6;
}

message NexusOperationFailureInfo {
    // The NexusOperationScheduled event ID.
    int64 scheduled_event_id = 1;
    // Endpoint name.
    string endpoint = 2;
    // Service name.
    string service = 3;
    // Operation name.
    string operation = 4;
    // Operation ID - may be empty if the operation completed synchronously.
    string operation_id = 5;
}

message Failure {
    string message = 1;
    // The source this Failure originated in, e.g. TypeScriptSDK / JavaSDK
    // In some SDKs this is used to rehydrate the stack trace into an exception object.
    string source = 2;
    string stack_trace = 3;
    // Alternative way to supply `message` and `stack_trace` and possibly other attributes, used for encryption of
    // errors originating in user code which might contain sensitive information.
    // The `encoded_attributes` Payload could represent any serializable object, e.g. JSON object or a `Failure` proto
    // message.
    //
    // SDK authors: 
    // - The SDK should provide a default `encodeFailureAttributes` and `decodeFailureAttributes` implementation that:
    //   - Uses a JSON object to represent `{ message, stack_trace }`.
    //   - Overwrites the original message with "Encoded failure" to indicate that more information could be extracted.
    //   - Overwrites the original stack_trace with an empty string.
    //   - The resulting JSON object is converted to Payload using the default PayloadConverter and should be processed
    //     by the user-provided PayloadCodec
    //
    // - If there's demand, we could allow overriding the default SDK implementation to encode other opaque Failure attributes.
    // (-- api-linter: core::0203::optional=disabled --)
    temporal.api.common.v1.Payload encoded_attributes = 20;
    Failure cause = 4;
    oneof failure_info {
        ApplicationFailureInfo application_failure_info = 5;
        TimeoutFailureInfo timeout_failure_info = 6;
        CanceledFailureInfo canceled_failure_info = 7;
        TerminatedFailureInfo terminated_failure_info = 8;
        ServerFailureInfo server_failure_info = 9;
        ResetWorkflowFailureInfo reset_workflow_failure_info = 10;
        ActivityFailureInfo activity_failure_info = 11;
        ChildWorkflowExecutionFailureInfo child_workflow_execution_failure_info = 12;
        NexusOperationFailureInfo nexus_operation_execution_failure_info = 13;
    }
}

message MultiOperationExecutionAborted {}
