Alokai

createHttpError

Factory function for creating HTTP errors. Available in integration context as context.createHttpError().

Signature

declare function createHttpError(
	options: CreateHttpErrorOptions
): HttpError;

Parameters

NameRequiredTypeDescription
optionsRequiredCreateHttpErrorOptionsError options

Returns

HttpError instance

Referenced Types

  • HttpError

Examples

Using with named status codes (recommended)

import { HttpStatusCode } from '@alokai/connect/middleware';

export async function getProduct(context, params) {
  if (!params.id) {
    throw context.createHttpError({
      statusCode: HttpStatusCode.BAD_REQUEST,
      message: 'Product ID is required',
      data: { field: 'id' }
    });
  }

  const product = await context.api.getProduct(params.id);

  if (!product) {
    throw context.createHttpError({
      statusCode: HttpStatusCode.NOT_FOUND,
      message: 'Product not found',
      statusMessage: 'Not Found'
    });
  }

  return product;
}

Using with numeric status codes

export async function updateProduct(context, params) {
  throw context.createHttpError({
    statusCode: 409,
    message: 'Product already exists'
  });
}

Input validation and wrapping downstream errors

export async function deleteProduct(context, params: { id: string }) {
  // Validate input first
  if (!params.id) {
    throw context.createHttpError({
      statusCode: HttpStatusCode.BAD_REQUEST,
      message: 'Product ID is required',
      data: { field: 'id' }
    });
  }

  // Wrap downstream API errors with cause for debugging
  try {
    await context.api.deleteProduct(params.id);
  } catch (error) {
    throw context.createHttpError({
      statusCode: HttpStatusCode.INTERNAL_SERVER_ERROR,
      message: 'Failed to delete product',
      cause: error // Preserves original error for logs
    });
  }
}

On this page