Handle errors in commands.

This commit is contained in:
Dillon Kearns 2024-04-15 09:04:08 -07:00
parent 2e7c5d1bdb
commit f7d42231d2
3 changed files with 34 additions and 3 deletions

View File

@ -37,6 +37,10 @@ run =
(\() ->
Expect.pass
)
, Stream.command "does-not-exist" []
|> Stream.run
|> expectError "command with error"
"Error: spawn does-not-exist ENOENT"
, BackendTask.Custom.run "hello"
Encode.null
Decode.string
@ -181,7 +185,10 @@ expectError name message task =
\() ->
case result of
Ok data ->
Expect.fail "Expected a failure, but got success!"
--Expect.fail "Expected a failure, but got success!"
result
|> Debug.toString
|> Expect.equal "Expected a failure, but got success!"
Err error ->
let

View File

@ -768,7 +768,6 @@ async function pipePartToStream(
retry: part.retries,
timeout: part.timeoutInMs,
});
// TODO what if there is an error?
let metadata = {
headers: Object.fromEntries(response.headers.entries()),
statusCode: response.status,
@ -799,6 +798,11 @@ async function pipePartToStream(
env: env,
});
newProcess.on("error", (error) => {
console.error("ERROR!");
resolve({ error: error.toString() });
});
lastStream && lastStream.pipe(newProcess.stdin);
let newStream;
if (output === "MergeWithStdout") {

View File

@ -272,8 +272,28 @@ run stream =
BackendTask.Internal.Request.request
{ name = "stream"
, body = BackendTask.Http.jsonBody (pipelineEncoder stream "none")
, expect = BackendTask.Http.expectJson (Decode.succeed ())
, expect =
BackendTask.Http.expectJson
(Decode.oneOf
[ Decode.field "error" Decode.string
|> Decode.andThen
(\error ->
Decode.succeed
(Err
(FatalError.recoverable
{ title = "Stream Error"
, body = error
}
(StreamError error)
)
)
)
, Decode.succeed (Ok ())
]
)
}
|> BackendTask.andThen BackendTask.fromResult
|> BackendTask.allowFatal
pipelineEncoder : Stream error metadata kind -> String -> Encode.Value