#cp.result

Represents the result of an operation which may end in success or failure. If it is a success, a value is typically provided. If it is a failure, a message is typically provided.

Using this type allows for more structured checking when performing an operation which may fail in a number of ways, rather than just calling error and crashing out. For example:

function clamped(value, min, max) if value < min then return result.failure("expected at least %d but got %d", min, value) elseif value > max then return result.failure("expected at most %d but got %d", max, value") else return result.success(value) end end local outcome = clamped(-1, 0, 100) if outcome.failure then error(outcome.message) end local value = outcome.value

Of course, simply checking the result and throwing an error is a common case, so you can achieve the same result like so:

local value = clamped(-1, 0, 100):get()

If you want to perform other tasks, check for .failure or .success and perform the appropriate response.


#API Overview

Functions - API calls offered directly by the extension

Constructors - API calls which return an object, typically one that offers API methods

Methods - API calls which can only be made on an object returned by a constructor


#API Documentation

#Functions

Signaturecp.result.is(value) -> boolean
TypeFunction
DescriptionChecks if the value is an instance of a cp.result.
Parameters
  • value - The value to check.
Returns
  • true if the value is an instance of cp.result.
NotesNone
ExamplesNone
Sourcesrc/extensions/cp//result.lua line 44

#Constructors

Signaturecp.result.failure(message) -> result
TypeConstructor
DescriptionCreates a new failure result, with the specified error message.
Parameters
  • message - Error message
Returns
  • A new result
NotesNone
ExamplesNone
Sourcesrc/extensions/cp//result.lua line 74

Signaturecp.result.from(value, err) -> result
TypeConstructor
DescriptionProvides a simple wrapper for the common value, err pattern of function error handling in Lua. If the err value is not nil, it will result in a failure, otherwise the value is passed to a success.
Parameters
  • value - The value if successful.
  • err - The error message if there was a failure.
Returns
  • A result.success or result.failure.
NotesNone
ExamplesNone
Sourcesrc/extensions/cp//result.lua line 91

Signaturecp.result.okValue(ok, value) -> result
TypeConstructor
DescriptionProvides a simple wrapper for the common ok, value|err pattern of function error handling in Lua. If ok is true, value is the successful result, otherwise value is the error message.
Parameters
  • ok - if true, the operation was successful.
  • value - ok is true, the successful value, otherwise the error message.
Returns
  • A result.success or result.failure.
NotesNone
ExamplesNone
Sourcesrc/extensions/cp//result.lua line 127

Signaturecp.result.success(value) -> result
TypeConstructor
DescriptionCreates a new success result, with the specified value.
Parameters
  • value - The specified value
Returns
  • A new result
NotesNone
ExamplesNone
Sourcesrc/extensions/cp//result.lua line 61

Signaturecp.result.valueErr(value, err) -> result
TypeConstructor
DescriptionProvides a simple wrapper for the common value, err pattern of function error handling in Lua. If the err is not nil it will result in a failure with the message, otherwise the value is passed to a success.
Parameters
  • value - The value if successful.
  • err - The error message if there was a failure.
Returns
  • A result.success or result.failure.
NotesNone
ExamplesNone
Sourcesrc/extensions/cp//result.lua line 109

#Methods

Signaturecp.result:get() -> anything
TypeMethod
DescriptionGets the successful value, or throws an error with the provided message.
Parameters
  • None
Returns
  • The value if it was a success, otherwise throws an error.
NotesNone
ExamplesNone
Sourcesrc/extensions/cp//result.lua line 145

Signaturecp.result:log([context]) -> cp.result
TypeMethod
DescriptionLogs the result to either the default channel (if success) or error channel (if failure), with the context string (if provided).
Parameters
  • context - A string that provides context for the logged value.
Returns
  • The same cp.result instance.
NotesNone
ExamplesNone
Sourcesrc/extensions/cp//result.lua line 178