Result<T> is the SDK's uniform return type for higher-level helpers (callBatch, B24HelperManager.loadData, OptionsManager.save, …). The class implements IResult<T>; the typed REST helpers (call.make, callList.make, …) return its specialised subclass AjaxResult<T>.
Usage
import { Result } from '@bitrix24/b24jssdk'
function parseConfig(raw: string): Result<{ theme: string }> {
try {
const data = JSON.parse(raw)
if (typeof data?.theme !== 'string') {
return Result.fail<{ theme: string }>('theme is required', 'config:theme')
}
return Result.ok({ theme: data.theme })
} catch (error) {
return Result.fail<{ theme: string }>(error as Error, 'config:json')
}
}
function applyConfig() {
const rawJson = '{"theme":"light"}'
const result = parseConfig(rawJson)
if (!result.isSuccess) {
console.error(result.getErrorMessages())
return
}
const { theme } = result.getData()!
console.log('theme:', theme)
}
applyConfig()
Constructor
new Result<T>(data?: T)
Default data is null.
Static Factories
Result.ok<U>(data?: U): Result<U>
Result.fail<U>(error: Error | string, key?: string): Result<U>
Getters
Data Methods
setData(data: T | null | undefined): Result<T>
getData(): T | null | undefined
setData is chainable. Note: AjaxResult disables setData and throws ReferenceError on call.
Error Methods
addError(error: Error | string, key?: string): Result<T>
addErrors(errors: (Error | string)[]): Result<T>
hasError(key: string): boolean
getErrors(): IterableIterator<Error>
getErrorMessages(): string[]
getErrorsByKey(): Record<string, Error>
getErrorMessagesByKey(): Record<string, string>
When key is omitted, addError generates a UUID v7 via Text.getUuidRfc4122() so duplicate strings still get distinct slots.
getErrorsByKey() / getErrorMessagesByKey() return a Record snapshot keyed by that identifier, so you can tell which call failed — unlike getErrors() / getErrorMessages(), which drop the keys, and unlike the live errors Map getter. For batches the key tells you which command failed: an object / named-command batch keys by the command label, and an array-mode batch keys by the command's numeric position ('0', '1', … — since v1.4.0 / #255; previously these were random UUIDs). An envelope-level soft error not tied to one command uses the internal base-error key, and addErrors() (no explicit key) still uses generated UUIDs — for those, prefer getErrors() / getErrorMessages().
const res = await $b24.actions.v2.batch.make({
calls: {
contact: { method: 'crm.item.get', params: { entityTypeId: 3, id: 1 } },
deal: { method: 'crm.item.get', params: { entityTypeId: 2, id: 999 } }
},
options: { isHaltOnError: false }
})
if (!res.isSuccess) {
console.error(res.getErrorMessagesByKey()) // e.g. { deal: 'Access denied' }
}
toString
toString(): string
Pretty-prints the result for logs:
- success →
Result(success): {<data JSON>} - failure →
Result(failure): {<data JSON>}\nErrors: <messages joined by ", ">
Error instances inside the data are serialised as { name, message, stack }. Failure to stringify falls back to '[Unable to serialize data]'.