Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle 404 for not running/existent sandbox #515

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 2 additions & 27 deletions packages/js-sdk/src/envd/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
AuthenticationError,
} from '../errors'
import { StartResponse, ConnectResponse } from './process/process_pb'
import { Code, ConnectError } from '@connectrpc/connect'
import { WatchDirResponse } from './filesystem/filesystem_pb'

export async function handleEnvdApiError<A, B, C extends `${string}/${string}`>(
Expand Down Expand Up @@ -50,19 +49,7 @@ export async function handleEnvdApiError<A, B, C extends `${string}/${string}`>(
export async function handleProcessStartEvent(
events: AsyncIterable<StartResponse | ConnectResponse>
) {
let startEvent: StartResponse | ConnectResponse

try {
startEvent = (await events[Symbol.asyncIterator]().next()).value
} catch (err) {
if (err instanceof ConnectError) {
if (err.code === Code.Unavailable) {
throw new NotFoundError('Sandbox is probably not running anymore')
}
}

throw err
}
const startEvent = (await events[Symbol.asyncIterator]().next()).value
if (startEvent.event?.event.case !== 'start') {
throw new Error('Expected start event')
}
Expand All @@ -73,19 +60,7 @@ export async function handleProcessStartEvent(
export async function handleWatchDirStartEvent(
events: AsyncIterable<WatchDirResponse>
) {
let startEvent: WatchDirResponse

try {
startEvent = (await events[Symbol.asyncIterator]().next()).value
} catch (err) {
if (err instanceof ConnectError) {
if (err.code === Code.Unavailable) {
throw new NotFoundError('Sandbox is probably not running anymore')
}
}

throw err
}
const startEvent = (await events[Symbol.asyncIterator]().next()).value
if (startEvent.event?.case !== 'start') {
throw new Error('Expected start event')
}
Expand Down
16 changes: 15 additions & 1 deletion packages/js-sdk/src/envd/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@ import { Code, ConnectError } from '@connectrpc/connect'
import { runtime } from '../api/metadata'
import { defaultUsername } from '../connectionConfig'

import { SandboxError, TimeoutError, formatSandboxTimeoutError, InvalidArgumentError, NotFoundError, AuthenticationError } from '../errors'
import {
AuthenticationError,
formatSandboxTimeoutError,
InvalidArgumentError,
NotFoundError,
SandboxError,
TimeoutError,
} from '../errors'


export function handleRpcError(err: unknown): Error {
Expand All @@ -24,6 +31,13 @@ export function handleRpcError(err: unknown): Error {
return new TimeoutError(
`${err.message}: This error is likely due to exceeding 'timeoutMs' — the total time a long running request (like command execution or directory watch) can be active. It can be modified by passing 'timeoutMs' when making the request. Use '0' to disable the timeout.`
)
case Code.Unimplemented:
// https://github.com/connectrpc/connect-es/blob/main/packages/connect/src/protocol-grpc/http-status.ts
if (err.rawMessage === 'HTTP 404') {
return new NotFoundError(`${err.message}: Sandbox is probably not running anymore.`)
} else {
return new SandboxError(`Not implemented: ${err.message}`)
}
default:
return new SandboxError(`${err.code}: ${err.message}`)
}
Expand Down
7 changes: 6 additions & 1 deletion packages/python-sdk/e2b_connect/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,12 @@ def error_for_response(http_resp: Response):
try:
error = json.loads(http_resp.content)
except (json.decoder.JSONDecodeError, KeyError):
if http_resp.status == 429:
if http_resp.status == 404:
return ConnectException(
Code.not_found,
f"HTTP {http_resp.status}: Sandbox is probably not running anymore.",
)
elif http_resp.status == 429:
return ConnectException(
Code.resource_exhausted,
f"{http_resp.content.decode()} The requests are being rate limited.",
Expand Down
Loading