Error handling

Custom error objects and messages will be returned to the used in case of a unexpected failure. Global error objects could be defined which could be overridden inside flows or resources.

error "proto.Error" {
	message = "{{ error:message }}"
	status = "{{ error:status }}"
}

flow "GlobalHandleError" {
	input "proto.Empty" {}

	resource "query" {
		request "proto.Service" "ThrowError" {
		}

		on_error {
			status = 401
			message = "global error message"
		}
	}

	output "proto.Empty" {}
}

Currently is it only possible to return a custom error object through the HTTP protocol. gRPC and GraphQL only support the ability to return a status code and error message.

Expected status code

A list of expected status codes defines which status codes could be expected to be returned from the given service. This prevents a resource from failing even if non OK status codes are returned. Multiple status codes could be expected and result in a pass.

flow "NotFound" {
	resource "query" {
		request "org.Service" "Prevent" {
			expect_status = [200, 404]
		}
	}
}

Error object

A custom error object could be defined inside a error block. This object is used to create a custom error object and headers which is returned to the user. All resources could be referenced inside the error object but no dependencies are created. It is advised to make use of the error.params resource to pass custom values such as trace ids, custom status codes etc.

Error objects could be defined as a global object, flow object or resource object. Each layer could override the parent layer. If a global object and flow object are defined is the flow object used for resource definitions.

error "org.Error" {
	header {
		X-Trace = "{{ error.params:trace }}"
	}

	message "meta" {
		trace = "{{ error.params:trace }}"
	}
		
	message = "{{ error:message }}"
	status = "{{ error:status }}"
}

On Error

A on error block could be defined which defines which status code and message should be returned to the user. Params could be defined with constant values or references to other resource. The error and error.params resource could be used inside error blocks to access these values. Schema definitions could be included which are used to decode the service error message. Service error message properties could be accessed through the resource.

on_error definitions could be overridden by flows and resources. If no on_error is defined is the parent definition used.

on_error {
  schema = "org.Schema"
  status = "{{ error:status }}"
  message = "custom error message"
  
  params {
    trace = "{{ trace:id }}"
    level = "critical"
    reason = "{{ resource.error:reason }}"
  }
}

Last updated