How to write MeshKit compatible errors

Meshery pervasively uses MeshKit as a golang and infrastructure management-specific library in all of its components. MeshKit helps populate error messages with a uniform and useful set of informative attributes.

To help with creating error codes, MeshKit contains a tool that analyzes, verifies and updates error codes in Meshery source code trees. It extracts error details into a file that can be used for publishing all error code references on the Meshery error codes reference page. The objective to create this was to avoid centralized handling of error codes and automating everything

In order to create a Meshery error object, you will need to create a custom wrapper object for the native golang error. This can be done from the MeshKit Error package.

This tool will create a couple of files, one of them is designed to be used to generate the error reference on the Meshery Documentation website. The file errorutil_analyze_summary.json contains a summary of the analysis, notably lists of duplicates etc.

Conventions to follow while creating errors

  1. Errors names and codes are namespaced to components, i.e. they need to be unique within a component, which is verified by this tool.
  2. Errors are not to be reused across components and modules.
  3. Error codes are not to be set as integer. CI will take care of updating Error codes from a string to an integer.
  4. Running make error analyzes your code and returns any warnings to be aware of.
  5. Capitalize the first letter of the every error description.
  6. Using errors.NewDefault(…) is deprecated. This tool emits a warning if its use is detected.
  7. Use errors.NewV3(…) for an error whose causes each need their own remediations (see Errors with several causes); otherwise errors.NewV2(…) or errors.New(…).
  8. Use errors.New(…) from MeshKit to create actual errors with all the details. This is often done in a factory function. It is important that the error code variable is used here, not a literal. Specify detailed descriptions, probable causes, and remedies. They need to be string literals, call expressions are ignored. This tool extracts this information from the code and exports it. For the Code argument in the errors.New use the same Error name and append a “Code” after it. e.g error name : ErrApplyManifest then the error code is ErrApplyManifestCode
  9. Set the value to any string, like “replace_me” (no convention here), e.g. ErrApplyManifestCode = “replace_me”.
  10. By convention, error codes and the factory functions live in files called error.go. The tool checks all files, but updates only error.go files.

Use the errors.New() function to create a new instance of the error object and pass situation-specific attributes as function arguments. These attributes are:

  • Code
  • Short Description
  • Long Description
  • Probable Cause
  • Suggested Remediation

Syntax

	
		errors.New(ErrExampleCode, errors.Alert, []string{"<short-description>"}, []string{"<long-description>"}, []string{"<probable-cause>"}, []string{"<suggested remediation>"})
	

Example

In this example we are creating an Error for being unable to marshal JSON

	
		var (
	// Error code
    ErrMarshalCode= "replace_me"

    //Static errors (for example)
    ErrExample = errors.New(ErrExampleCode, errors.Alert, []string{"<short-description>"}, []string{"<long-description>"}, []string{"<probable-cause>"}, []string{"<suggested remediation>"})
)

// Dynamic errors
//Error Name
func ErrMarshal(err error, obj string) error {
	return errors.New(ErrMarshalCode, errors.Alert, []string{"Unable to marshal the : ", obj}, []string{err.Error()}, []string{}, []string{})
}
	

Replacing old Error Codes

Old

	
		bd, err := json.Marshal(providers)
	if err != nil {
		http.Error(w, "unable to marshal the providers", http.StatusInternalServerError)
		return
	}
	

New

	
		bd, err := json.Marshal(providers)
  if err != nil {
          marshalErr := ErrMarshal(err, "providers")
          h.log.Error(marshalErr)
          writeMeshkitError(w, marshalErr, http.StatusInternalServerError)
          return
      }
	

http.Error is rejected by CI in the ./server module: it writes a plain-text body and strips the MeshKit code, severity, and remediation that clients parse. See HTTP Error Response Contract for the response shape, the writeMeshkitError / writeJSONError helpers, and how to choose the status code.

Errors with several causes: errors.NewV3

errors.New takes probable causes and suggested remediations as two flat lists, and nothing tells a reader which remediation answers which cause. When an error has causes that each need their own remediations, declare it with errors.NewV3. Each cause then carries the remediations that address it, and a remediation can name an action a client may run and the policy for running it (error definition contract).

	
		// remRetryConnection answers every cause, so it is declared once.
var remRetryConnection = errors.Remediation{
	ID:          "retry-connection",
	Description: "Once the cause is fixed, reconnect the connection.",
	Action:      "connection.reconnect",
	ActionPolicy: errors.RequiresApproval,
}

func ErrUnreachableCluster(err error, server string) error {
	e := errors.NewV3(ErrUnreachableClusterCode, errors.Alert,
		[]string{"Error communicating with the Kubernetes API server."},
		[]string{"Meshery Server did not get an answer from the cluster's API server."},
		[]errors.Cause{
			{
				ID:          "api-server-unreachable",
				Description: "The Kubernetes API server is not reachable.",
				Remediations: []errors.Remediation{
					{ID: "check-network", Description: "Verify network connectivity to the API server."},
					remRetryConnection,
				},
			},
			{
				ID:          "credentials-rejected",
				Description: "The cluster rejected the connection's credentials.",
				Remediations: []errors.Remediation{
					{ID: "renew-credential", Description: "Renew the connection's credential.",
						Action: "connection.refresh-credentials", ActionPolicy: errors.Automatic},
					remRetryConnection,
				},
			},
		})
	// Instance text is appended after NewV3 returns.
	e.LongDescription = append(e.LongDescription, fmt.Sprintf("API server: %s", server), err.Error())
	return e
}
	

NewV3 returns a *errors.ErrorV2 whose Causes hold the nesting and whose flat ProbableCause and SuggestedRemediation are derived from it. GetCause, GetRemedy, Error() and every reader of the flat lists keep working.

errorutil reads these definitions from source, so they follow strict rules:

  • Literals only. Write every errors.Cause and errors.Remediation as a keyed composite literal with string-literal ID, Description and Action. Every element of the short- and long-description lists is a string literal too: build instance text, such as an address or err.Error(), into LongDescription after NewV3 returns, as above.
  • Stable ids. Ids are lowercase words joined by hyphens (api-server-unreachable) and never change once published: events name the causes that applied in metadata.error.matchedCauses, and each code’s reference page anchors on them. Cause ids are unique within an error, and every cause has at least one remediation.
  • Shared remediations. A remediation that answers several causes is declared once as a package-level var (in any file of the package) and referenced from each cause. errorutil resolves the variable, and the flat remediation list carries it once.
  • Action policies. errors.Manual (also when empty) means a person applies the remediation. errors.RequiresApproval lets a client run the Action after a person approves it, and errors.Automatic lets it run the Action on its own and say so. Both need an Action, dot-separated lowercase words such as connection.reconnect. An unknown policy is never coerced: errors.ValidateCauses and errorutil reject it, and a reader treats it as manual.
  • Tests. Unit-test a definition with errors.ValidateCauses(errors.GetCauses(err)). TestErrUnreachableKubeAPICauses in server/models/event_error_test.go is an example.

An invalid definition fails the build. errorutil lists every NewV3 definition it rejects in errorutil_analyze_summary.json under invalid_definitions, logs each one with its problems, leaves it out of both exports (never in a reduced form), and exits non-zero from analyze and update. The error-codes-updater workflow then fails the pull request. errors.New and errors.NewV2 definitions are unaffected.

When the server can tell which cause applies to an occurrence, record it where the underlying error is still in hand, so the error event names it. ErrUnreachableKubeAPI (server/models/error.go) classifies the Kubernetes client error, and models.AttachEventError puts the matched cause on the event; see Contributing to Meshery Server Events.

Replacing logrus

There already exists an interface for logger in MeshKit.

Defining a Logger

	
		type Logger struct {
	log   logger.Handler
}
	

Debug

Old
	
		logrus.Debugf("meshLocationURL: %s", meshLocationURL)
	
New
	
		l.log.Debug("meshLocationURL: ", meshLocationURL)
	

Error

Old
	
		logrus.Errorf("error marshaling data: %v.", err)
	
New
	
		l.log.Error(ErrMarshal(err, obj))
	

A small program using meshkit errors and logger

	
		package main

import (
	"fmt"
	"os"

	meshkitErrors "github.com/meshery/meshkit/errors"
	"github.com/meshery/meshkit/logger"
	"github.com/sirupsen/logrus"
	"github.com/spf13/viper"
)

var (
	// CI will replace test_code with new error code
	ErrOpeningFileCode = "test_code"
)

func main() {
	logLevel := viper.GetInt("LOG_LEVEL")
	if viper.GetBool("DEBUG") {
		logLevel = int(logrus.DebugLevel)
	}
	log, err := logger.New("test", logger.Options{
		Format:   logger.SyslogLogFormat,
		LogLevel: logLevel,
	})
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	// logging meshkit error
	err = openFileWithMeshkitError("some.txt")
	if err != nil {
		log.Error(err)
	}
	// OUTPUT
	// ERRO[2021-11-10T17:31:28+05:30] open some.txt: no such file or directory
	// app=test code=1001 probable-cause="empty string passed as argument .file with this name doesn't exist"
	// severity=2 short-description="unable to open file" suggested-remediation="pass a non-empty string as
	// filename .create file before opening it"

	// logging non meshkit error
	err = openFile("some.txt")
	if err != nil {
		log.Error(err)
	}
	// OUTPUT
	// ERRO[2024-07-01T19:09:09+05:30] open some.txt: no such file or directory
	// app=test code= probable-cause= severity=0 short-description= suggested-remediation=

}

// this returns a non meshkit error
func openFile(name string) error {
	_, err := os.Open(name)
	return err
}

// this returns a meshkit error
func openFileWithMeshkitError(name string) error {
	_, err := os.Open(name)
	return ErrOpeningFile(err)
}

func ErrOpeningFile(err error) error {
	return meshkitErrors.New(ErrOpeningFileCode, meshkitErrors.Alert, []string{"unable to open file"},
		[]string{err.Error()},
		[]string{"empty string passed as argument ", "file with this name doesn't exist"},
		[]string{"pass a non-empty string as filename ", "create file before opening it"})
}
	

Generating error codes in meshery/meshery

make error regenerates codes for the server but skips mesheryctl. The two components keep the same contract in different files.

  • mesheryctl: take the next code from mesheryctl/helpers/component_info.json (next_error_code) and bump that value in the same commit.
  • Server: the same contract lives in server/helpers/component_info.json. errorutil refuses to run at all - “next_error_code is lower than or equal to highest used code” - until next_error_code is bumped past every code you added, so bump it in the same commit.

.github/workflows/error-codes-updater.yaml re-runs errorutil on every pull request and fails it if the analysis reports anything.

Naming and formatting

Name each constant <BuilderFuncName>Code - errorutil keys the export off that pairing. Adding a constant longer than the block’s current widest name makes gofmt realign the entire error.go const block, so prefer a shorter name over a 300-line whitespace diff.

Regenerating the docs reference

errorutil writes four files to the output directory, all gitignored: errorutil_analyze_errors.json and errorutil_analyze_summary.json (the analysis: duplicates, convention violations and invalid_definitions) and two exports:

  • errorutil_errors_export.json, the legacy export. Its shape is frozen (snake_case keys, joined strings) because the docs shortcodes read it; NewV3 entries additionally carry probableCauses.
  • errorutil_errors_index.json, the component’s definitions as a schemas ErrorReferenceIndex (schemaVersion 2): code (meshery-server-1304), moniker, component, numericCode, severity, descriptions, nested causes and the derived flat lists. errorutil validates every entry against the schema before writing it; the docs build assigns referenceUrl.

The reference data built from them is tracked:

  • docs/data/errorref/meshery-server_errors_export.json, from the legacy export. Regenerate it with the jq --slurpfile wrapper the workflow uses, or the published error reference silently omits the new codes.
  • docs/data/errorindex/meshery-server_errors_index.json, from the nested export (jq . server/helpers/errorutil_errors_index.json). The docs build takes a code’s causes from it, else from the legacy export’s probableCauses, and otherwise shows the flat lists.

The workflow regenerates both. The docs build publishes every component’s codes as reference/error-codes/index.json, a schemas ErrorReferenceIndex (schemaVersion 2).

Rendering errors to the user

Only utils.Log.Error(err) renders a MeshKit error’s code, cause and remediation; cobra’s default print shows just the message. In mesheryctl commands, log the structured error for the user and return it for the exit path.