syntax = "proto2";

package wix.api;

import "google/protobuf/descriptor.proto";
import "wix/api/annotations.proto";

option java_multiple_files = true;
option java_outer_classname = "ApiErrorsProto";
option java_package = "com.wix.api";

/**
* Annotation for method returned errors.
* Example usage:
**/
//import "google/protobuf/empty.proto";
//service FooService {
//  // Definition of a service-level error.
//  // Accepts `markdown` and multiline.
//  option (wix.api.error_def) = {
//    http_code: FAILED_PRECONDITION
//    application_code: "NO_FOO_SUPPLY"
//  };
//
//  rpc CreateFoo (google.protobuf.Empty) returns (google.protobuf.Empty) {
//    // Reference to a service-level error.
//    option (wix.api.error_ref) = {
//      application_code: "NO_FOO_SUPPLY"
//    };
//    // Definition of a method-level error.
//    // Accepts `markdown` and multiline.
//    option (wix.api.error) = {
//      http_code: FAILED_PRECONDITION
//      application_code: "NO_FOO_SUPPLY"
//      data: "com.wixpress.foo.v1.NoFooSupplyError"
//    };
//    // You can specify multiple errors.
//    option (wix.api.custom_validation_error) = {
//      rule_name: "INVALID_NOTE"
//      data: "com.wixpress.foo.v1.MyRuleViolationError"
//    };
//    // This method MAY throw standard crud errors of BULK_UPDATE method type.
//    option (wix.api.inherit_crud_errors) = {
//      method: BULK_UPDATE
//    };
//  }
//}

extend google.protobuf.ServiceOptions {
  repeated Error error_def = 50600;
}

extend google.protobuf.MethodOptions {
  repeated Error error = 50801;
  repeated CustomValidationError custom_validation_error = 50802;
  repeated InheritCrudErrors inherit_crud_errors = 50803;
  repeated ErrorRef error_ref = 50804;
}

/**
  ApplicationError: named Error for backwards compatibility
**/
message Error {
  required StatusCodes.HttpStatusCode http_code = 1;
  optional string application_code = 2;
  optional string data = 3; // FQN path to the message that will be sent as payload for this error
}

/**
  Method-level reference to a service-level error.
**/
message ErrorRef {
  // Must match existing service-level error application code.
  required string application_code = 1;
}

/**
  CustomValidationError: field level validation, only declare when you have specific validation in your service that is not coming from infra
**/
message CustomValidationError {
  required string rule_name = 1;
  optional string data = 2; // FQN path to the message that will be sent as payload for this error
}

/**
  InheritCrudErrors: does this method throw standard crud errors?
**/
message InheritCrudErrors {
  required Crud.Method method = 1; // types of crud methods to inherit default errors from
}

message StatusCodes {
  enum HttpStatusCode {
    OK = 0; // 200

    //Default error codes (applicable to all endpoints)

    // 401 - Identity missing (missing, invalid or expired oAuth token,
    // signed instance or cookies)
    UNAUTHENTICATED = 1;

    // 403 - Identity does not have the permission needed for this method / resource
    PERMISSION_DENIED = 2;

    // 400 - Bad Request. The client sent malformed body
    // or one of the arguments was invalid
    INVALID_ARGUMENT = 3;

    // 404 - Resource does not exist
    NOT_FOUND = 4;

    // 500 - Internal Server Error
    INTERNAL = 5;

    // 503 - Come back later, server is currently unavailable
    UNAVAILABLE = 6;

    // 429 - The client has sent too many requests
    // in a given amount of time (rate limit)
    RESOURCE_EXHAUSTED = 7;

    //Custom error codes - need to be documented

    // 499 - Request cancelled by the client
    CANCELED = 8;

    // 409 - Can't recreate same resource or concurrency conflict
    ALREADY_EXISTS = 9; //409

    // 428 - request cannot be executed in current system state
    // such as deleting a non-empty folder or paying with no funds
    FAILED_PRECONDITION = 10;

    //DO NOT USE IN WIX
    // ABORTED = 11; // 409
    // OUT_OF_RANGE = 12; // 400
    // DEADLINE_EXEEDED = 13; // 504
    // DATA_LOSS = 14; // 500
    // UNIMPLEMENTED = 15; // 501
  }
}

