ErrorHandler

Overview

ErrorHandler is a error handler resource that is used for handling errors ocurred while processing requests.

Config yaml format becomes like below. And the resource specific spec is defined in in the proto format shown in the Resource Definition.

apiVersion: core/v1
kind: ErrorHandler
metadata:
  name: "default"
  namespace: "default"
spec: {}

Resource Definition

ErrorHandler is defined in the proto/core/v1/errhandler.proto

 1syntax = "proto3";
 2package core.v1;
 3
 4import "buf/validate/validate.proto";
 5import "core/v1/template.proto";
 6import "kernel/resource.proto";
 7
 8option go_package = "github.com/aileron-gateway/aileron-gateway/apis/core/v1";
 9
10// ErrorHandler resource definition.
11// apiVersion="core/v1", kind="ErrorHandler".
12message ErrorHandler {
13    string           APIVersion = 1 [json_name = "apiVersion"];
14    string           Kind       = 2 [json_name = "kind"];
15    kernel.Metadata  Metadata   = 3 [json_name = "metadata"];
16    ErrorHandlerSpec Spec       = 4 [json_name = "spec"];
17}
18
19// ErrorHandlerSpec is the specifications for the ErrorHandler object.
20message ErrorHandlerSpec {
21    // [OPTIONAL]
22    // StackAlways is the flag to output stacktrace to the logger.
23    // If set to true, this error handler output stacktrace
24    // even when handling client side error, or 400-499 status error.
25    // Default is [false].
26    bool StackAlways = 1 [json_name = "stackAlways"];
27
28    // [OPTIONAL]
29    // ErrorMessages is the list of custom error messages to overwrite.
30    // Default is not set.
31    repeated ErrorMessageSpec ErrorMessages = 2 [json_name = "errorMessages"];
32}
33
34// ErrorMessageSpec is the specification of HTTP error response.
35message ErrorMessageSpec {
36    // [OPTIONAL]
37    // Codes are the list of error code patterns to match this message.
38    // String is evaluated by the path match.
39    // For example, "E123*" or "E12*".
40    // See https://pkg.go.dev/path#Match for for available expressions.
41    // If an error matched to one of the Codes, Kinds or Message patterns
42    // then the response is overwritten by the MIMEContents.
43    // Default is not set.
44    repeated string Codes = 2 [json_name = "codes", (buf.validate.field).repeated.unique = true, (buf.validate.field).repeated.items.string.min_len = 1];
45
46    // [OPTIONAL]
47    // Kinds are the list of error kind patterns to match this message.
48    // String is evaluated by the path match.
49    // See https://pkg.go.dev/path#Match for for available expressions.
50    // If an error matched to one of the Codes, Kinds or Message patterns
51    // then the response is overwritten by the MIMEContents.
52    // Default is not set.
53    repeated string Kinds = 3 [json_name = "kinds", (buf.validate.field).repeated.unique = true, (buf.validate.field).repeated.items.string.min_len = 1];
54
55    // [OPTIONAL]
56    // Messages are the list of error message pattarns to match this message.
57    // String is evaluated by the regular expression.
58    // See https://pkg.go.dev/regexp and https://github.com/google/re2/wiki/Syntax
59    // for available expressions.
60    // If an error matched to one of the Codes, Kinds or Message patterns
61    // then the response is overwritten by the MIMEContents.
62    // Default is not set.
63    repeated string Messages = 4 [json_name = "messages", (buf.validate.field).repeated.unique = true, (buf.validate.field).repeated.items.string.min_len = 1];
64
65    // [OPTIONAL]
66    // HeaderTemplate is the key-value pairs of HTTP headers
67    // to add to the error response.
68    // Unlike headers that can set in MIMEContents field,
69    // values can be written in template.
70    // HTTP status code `{{status}}` and status text `{{statusText}}`,
71    // error code and kind `{{code}}`, `{{kind}}` can be used in the value.
72    // Header names cannot be in template format.
73    // This field is mainly intended to set error redirecting headers.
74    // Default is not set.
75    map<string, string> HeaderTemplate = 5 [json_name = "headerTemplate"];
76
77    // [OPTIONAL]
78    // MIMEContents is the list of mime content to be used for overwriting the error response.
79    // If an error matched to one of the Codes, Kinds or Message patterns
80    // then the response is overwritten by one of the MIMEContents.
81    // Responses are not overwritten if this field has no content.
82    // The first one is used when the Accept header did not matched to any content.
83    // Default is not set.
84    repeated MIMEContentSpec MIMEContents = 6 [json_name = "mimeContents"];
85}

最終更新 June 7, 2025: add japanese (f2a41f1)