Skip to content

Commit

Permalink
ws error (#736)
Browse files Browse the repository at this point in the history
  • Loading branch information
justcoon authored Aug 9, 2024
1 parent b02cb6c commit 651aacd
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 134 deletions.
38 changes: 23 additions & 15 deletions golem-cli/src/oss/clients/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,18 @@
use std::time::Duration;

use crate::clients::worker::WorkerClient;
use crate::model::{
GolemError, IdempotencyKey, WorkerMetadata, WorkerName, WorkerUpdateMode,
WorkersMetadataResponse,
};
use async_trait::async_trait;
use futures_util::{future, pin_mut, SinkExt, StreamExt};
use golem_client::api::WorkerError;
use golem_client::model::{
InvokeParameters, InvokeResult, ScanCursor, UpdateWorkerRequest, WorkerCreationRequest,
WorkerFilter, WorkerId, WorkersMetadataRequest,
};
use golem_client::Context;
use golem_client::{Context, Error};
use golem_common::uri::oss::urn::{ComponentUrn, WorkerUrn};
use native_tls::TlsConnector;
use serde::Deserialize;
Expand All @@ -31,11 +36,6 @@ use tokio_tungstenite::tungstenite::protocol::Message;
use tokio_tungstenite::{connect_async_tls_with_config, Connector};
use tracing::{debug, info};

use crate::model::{
GolemError, IdempotencyKey, WorkerMetadata, WorkerName, WorkerUpdateMode,
WorkersMetadataResponse,
};

#[derive(Clone)]
pub struct WorkerClientLive<C: golem_client::api::WorkerClient + Sync + Send> {
pub client: C,
Expand Down Expand Up @@ -265,16 +265,11 @@ impl<C: golem_client::api::WorkerClient + Sync + Send> WorkerClient for WorkerCl
.await
.map_err(|e| match e {
tungstenite::error::Error::Http(http_error_response) => {
let status = http_error_response.status().as_u16();

match http_error_response.body().clone() {
Some(body) => GolemError(format!(
"Failed Websocket. Http error: {}, {}",
http_error_response.status(),
String::from_utf8_lossy(&body)
)),
None => GolemError(format!(
"Failed Websocket. Http error: {}",
http_error_response.status()
)),
Some(body) => get_worker_golem_error(status, body),
None => GolemError(format!("Failed Websocket. Http error: {}", status)),
}
}
_ => GolemError(format!("Failed Websocket. Error: {}", e)),
Expand Down Expand Up @@ -429,3 +424,16 @@ struct Log {
pub context: String,
pub message: String,
}

fn get_worker_golem_error(status: u16, body: Vec<u8>) -> GolemError {
let error: Result<Error<WorkerError>, serde_json::Error> = match status {
400 => serde_json::from_slice(&body).map(|body| Error::Item(WorkerError::Error400(body))),
401 => serde_json::from_slice(&body).map(|body| Error::Item(WorkerError::Error401(body))),
403 => serde_json::from_slice(&body).map(|body| Error::Item(WorkerError::Error403(body))),
404 => serde_json::from_slice(&body).map(|body| Error::Item(WorkerError::Error404(body))),
409 => serde_json::from_slice(&body).map(|body| Error::Item(WorkerError::Error409(body))),
500 => serde_json::from_slice(&body).map(|body| Error::Item(WorkerError::Error500(body))),
_ => Ok(Error::unexpected(status, body.into())),
};
error.unwrap_or_else(Error::from).into()
}
96 changes: 30 additions & 66 deletions golem-service-base/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1436,117 +1436,81 @@ impl TryFrom<golem_api_grpc::proto::golem::worker::v1::WorkerExecutionError> for
}

impl From<GolemError> for golem_api_grpc::proto::golem::worker::v1::WorkerExecutionError {
fn from(error: GolemError) -> Self {
golem_api_grpc::proto::golem::worker::v1::WorkerExecutionError {
error: Some(error.into()),
}
}
}

impl From<GolemError> for golem_api_grpc::proto::golem::worker::v1::worker_execution_error::Error {
fn from(error: GolemError) -> Self {
match error {
GolemError::InvalidRequest(err) => {
golem_api_grpc::proto::golem::worker::v1::WorkerExecutionError {
error: Some(golem_api_grpc::proto::golem::worker::v1::worker_execution_error::Error::InvalidRequest(err.into())),
}
golem_api_grpc::proto::golem::worker::v1::worker_execution_error::Error::InvalidRequest(err.into())
}
GolemError::WorkerAlreadyExists(err) => {
golem_api_grpc::proto::golem::worker::v1::WorkerExecutionError {
error: Some(golem_api_grpc::proto::golem::worker::v1::worker_execution_error::Error::WorkerAlreadyExists(err.into())),
}
golem_api_grpc::proto::golem::worker::v1::worker_execution_error::Error::WorkerAlreadyExists(err.into())
}
GolemError::WorkerNotFound(err) => {
golem_api_grpc::proto::golem::worker::v1::WorkerExecutionError {
error: Some(golem_api_grpc::proto::golem::worker::v1::worker_execution_error::Error::WorkerNotFound(err.into())),
}
golem_api_grpc::proto::golem::worker::v1::worker_execution_error::Error::WorkerNotFound(err.into())
}
GolemError::WorkerCreationFailed(err) => {
golem_api_grpc::proto::golem::worker::v1::WorkerExecutionError {
error: Some(golem_api_grpc::proto::golem::worker::v1::worker_execution_error::Error::WorkerCreationFailed(err.into())),
}
golem_api_grpc::proto::golem::worker::v1::worker_execution_error::Error::WorkerCreationFailed(err.into())
}
GolemError::FailedToResumeWorker(err) => {
golem_api_grpc::proto::golem::worker::v1::WorkerExecutionError {
error: Some(golem_api_grpc::proto::golem::worker::v1::worker_execution_error::Error::FailedToResumeWorker(Box::new(err.into()))),
}
golem_api_grpc::proto::golem::worker::v1::worker_execution_error::Error::FailedToResumeWorker(Box::new(err.into()))
}
GolemError::ComponentDownloadFailed(err) => {
golem_api_grpc::proto::golem::worker::v1::WorkerExecutionError {
error: Some(golem_api_grpc::proto::golem::worker::v1::worker_execution_error::Error::ComponentDownloadFailed(err.into())),
}
golem_api_grpc::proto::golem::worker::v1::worker_execution_error::Error::ComponentDownloadFailed(err.into())
}
GolemError::ComponentParseFailed(err) => {
golem_api_grpc::proto::golem::worker::v1::WorkerExecutionError {
error: Some(golem_api_grpc::proto::golem::worker::v1::worker_execution_error::Error::ComponentParseFailed(err.into())),
}
golem_api_grpc::proto::golem::worker::v1::worker_execution_error::Error::ComponentParseFailed(err.into())
}
GolemError::GetLatestVersionOfComponentFailed(err) => {
golem_api_grpc::proto::golem::worker::v1::WorkerExecutionError {
error: Some(golem_api_grpc::proto::golem::worker::v1::worker_execution_error::Error::GetLatestVersionOfComponentFailed(err.into())),
}
golem_api_grpc::proto::golem::worker::v1::worker_execution_error::Error::GetLatestVersionOfComponentFailed(err.into())
}
GolemError::PromiseNotFound(err) => {
golem_api_grpc::proto::golem::worker::v1::WorkerExecutionError {
error: Some(golem_api_grpc::proto::golem::worker::v1::worker_execution_error::Error::PromiseNotFound(err.into())),
}
golem_api_grpc::proto::golem::worker::v1::worker_execution_error::Error::PromiseNotFound(err.into())
}
GolemError::PromiseDropped(err) => {
golem_api_grpc::proto::golem::worker::v1::WorkerExecutionError {
error: Some(golem_api_grpc::proto::golem::worker::v1::worker_execution_error::Error::PromiseDropped(err.into())),
}
golem_api_grpc::proto::golem::worker::v1::worker_execution_error::Error::PromiseDropped(err.into())
}
GolemError::PromiseAlreadyCompleted(err) => {
golem_api_grpc::proto::golem::worker::v1::WorkerExecutionError {
error: Some(golem_api_grpc::proto::golem::worker::v1::worker_execution_error::Error::PromiseAlreadyCompleted(err.into())),
}
golem_api_grpc::proto::golem::worker::v1::worker_execution_error::Error::PromiseAlreadyCompleted(err.into())
}
GolemError::Interrupted(err) => {
golem_api_grpc::proto::golem::worker::v1::WorkerExecutionError {
error: Some(golem_api_grpc::proto::golem::worker::v1::worker_execution_error::Error::Interrupted(err.into())),
}
golem_api_grpc::proto::golem::worker::v1::worker_execution_error::Error::Interrupted(err.into())
}
GolemError::ParamTypeMismatch(err) => {
golem_api_grpc::proto::golem::worker::v1::WorkerExecutionError {
error: Some(golem_api_grpc::proto::golem::worker::v1::worker_execution_error::Error::ParamTypeMismatch(err.into())),
}
golem_api_grpc::proto::golem::worker::v1::worker_execution_error::Error::ParamTypeMismatch(err.into())
}
GolemError::NoValueInMessage(err) => {
golem_api_grpc::proto::golem::worker::v1::WorkerExecutionError {
error: Some(golem_api_grpc::proto::golem::worker::v1::worker_execution_error::Error::NoValueInMessage(err.into())),
}
golem_api_grpc::proto::golem::worker::v1::worker_execution_error::Error::NoValueInMessage(err.into())
}
GolemError::ValueMismatch(err) => {
golem_api_grpc::proto::golem::worker::v1::WorkerExecutionError {
error: Some(golem_api_grpc::proto::golem::worker::v1::worker_execution_error::Error::ValueMismatch(err.into())),
}
golem_api_grpc::proto::golem::worker::v1::worker_execution_error::Error::ValueMismatch(err.into())
}
GolemError::UnexpectedOplogEntry(err) => {
golem_api_grpc::proto::golem::worker::v1::WorkerExecutionError {
error: Some(golem_api_grpc::proto::golem::worker::v1::worker_execution_error::Error::UnexpectedOplogEntry(err.into())),
}
golem_api_grpc::proto::golem::worker::v1::worker_execution_error::Error::UnexpectedOplogEntry(err.into())
}
GolemError::RuntimeError(err) => {
golem_api_grpc::proto::golem::worker::v1::WorkerExecutionError {
error: Some(golem_api_grpc::proto::golem::worker::v1::worker_execution_error::Error::RuntimeError(err.into())),
}
golem_api_grpc::proto::golem::worker::v1::worker_execution_error::Error::RuntimeError(err.into())
}
GolemError::InvalidShardId(err) => {
golem_api_grpc::proto::golem::worker::v1::WorkerExecutionError {
error: Some(golem_api_grpc::proto::golem::worker::v1::worker_execution_error::Error::InvalidShardId(err.into())),
}
golem_api_grpc::proto::golem::worker::v1::worker_execution_error::Error::InvalidShardId(err.into())
}
GolemError::PreviousInvocationFailed(err) => {
golem_api_grpc::proto::golem::worker::v1::WorkerExecutionError {
error: Some(golem_api_grpc::proto::golem::worker::v1::worker_execution_error::Error::PreviousInvocationFailed(err.into())),
}
golem_api_grpc::proto::golem::worker::v1::worker_execution_error::Error::PreviousInvocationFailed(err.into())
}
GolemError::PreviousInvocationExited(err) => {
golem_api_grpc::proto::golem::worker::v1::WorkerExecutionError {
error: Some(golem_api_grpc::proto::golem::worker::v1::worker_execution_error::Error::PreviousInvocationExited(err.into())),
}
golem_api_grpc::proto::golem::worker::v1::worker_execution_error::Error::PreviousInvocationExited(err.into())
}
GolemError::Unknown(err) => {
golem_api_grpc::proto::golem::worker::v1::WorkerExecutionError {
error: Some(golem_api_grpc::proto::golem::worker::v1::worker_execution_error::Error::Unknown(err.into())),
}
golem_api_grpc::proto::golem::worker::v1::worker_execution_error::Error::Unknown(err.into())
}
GolemError::InvalidAccount(err) => {
golem_api_grpc::proto::golem::worker::v1::WorkerExecutionError {
error: Some(golem_api_grpc::proto::golem::worker::v1::worker_execution_error::Error::InvalidAccount(err.into())),
}
golem_api_grpc::proto::golem::worker::v1::worker_execution_error::Error::InvalidAccount(err.into())
}
}
}
Expand Down
Loading

0 comments on commit 651aacd

Please sign in to comment.