Skip to content

Connection failures produce empty error message #99

Description

@MattDevy

Bug

When Elasticsearch is unreachable (connection refused, DNS failure, timeout), the CLI produces an error payload with an empty message:

{
  "error": {
    "code": "transport_error",
    "message": ""
  }
}

The user gets no information about what went wrong.

Steps to reproduce

# Point at a port with nothing listening
elastic es --config-file <(cat <<EOF
current_context: broken
contexts:
  broken:
    elasticsearch:
      url: http://localhost:19999
      auth:
        api_key: fake
EOF
) info

Output:

{"error":{"code":"transport_error","message":""}}

Root cause

src/es/handler.ts:96-98:

const message = err instanceof Error ? err.message : String(err)
return { error: { code: 'transport_error', message } }

The @elastic/transport library throws a ConnectionError (which extends Error) when the connection is refused. The message property on connection errors can be empty or contain only the low-level error code. The handler doesn't extract the cause chain or the underlying system error.

Expected behavior

The error message should indicate what went wrong, e.g.:

{
  "error": {
    "code": "transport_error",
    "message": "Connection refused: http://localhost:19999"
  }
}

Or at minimum, include the error name/type and the target URL so the user knows what failed.

Suggested fix

Inspect the error's cause chain and construct a more helpful message:

function transportError(err: unknown): JsonValue {
  if (err instanceof errors.ResponseError) {
    return { ... }  // existing handling
  }
  if (err instanceof errors.ConnectionError) {
    const url = err.meta?.connection?.url?.toString() ?? 'unknown'
    const reason = err.message || err.cause?.message || 'connection failed'
    return { error: { code: 'connection_error', message: `${reason} (${url})` } }
  }
  // ...
}

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No fields configured for Bug.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions