Skip to content

Commit 51a901a

Browse files
committed
Real error messages for Terminal.Run
1 parent 2076c3a commit 51a901a

File tree

2 files changed

+66
-22
lines changed

2 files changed

+66
-22
lines changed

src/Main.gren

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -278,9 +278,9 @@ update msg model =
278278
}
279279
|> Task.onError
280280
(\error ->
281-
-- TODO: Terminal.Run.errorToString
282-
-- TODO: Why am I not seeing the write to stderr?
283-
Stream.Log.line model.stderr (Debug.toString error)
281+
Terminal.Run.prettifyError error
282+
|> Terminal.Help.prettyPrint { useColor = model.useColor }
283+
|> Stream.Log.line model.stderr
284284
|> Task.map (\_ -> Node.exitWithCode 1)
285285
)
286286
|> Task.executeCmd
@@ -497,10 +497,11 @@ parseUserArgs model compilerPath =
497497
)
498498
}
499499
|> Task.onError
500-
(\err ->
501-
Stream.Log.line model.stdout (Debug.toString err)
502-
|> Task.execute
503-
|> Task.succeed
500+
(\error ->
501+
Terminal.Run.prettifyError error
502+
|> Terminal.Help.prettyPrint { useColor = model.useColor }
503+
|> Stream.Log.line model.stderr
504+
|> Task.map (\_ -> Node.exitWithCode 1)
504505
)
505506
|> Task.executeCmd
506507

src/Terminal/Run.gren

Lines changed: 58 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ module Terminal.Run exposing
22
( Error
33
, make
44
, run
5+
, prettifyError
56
)
67

78
{-| Support the `gren run ModuleName` command.
89
-}
910

11+
import CLI.PrettyPrinter as PP
1012
import Compiler.PackageName as PackageName exposing (PackageName)
1113
import Compiler.Backend as Backend
1214
import Bytes exposing (Bytes)
@@ -24,17 +26,13 @@ import Process
2426
import Stream
2527
import Task exposing (Task)
2628
import Terminal.PackageInstall as PackageInstall exposing (PackageResolution)
29+
import Terminal.Help as Help
2730

2831

2932
type Error
3033
= TempPathError FileSystem.Error
3134
| ReadProjectOutlineError PackageInstall.ReadProjectOutlineError
32-
| ReadOutlineError FileSystem.Error
33-
| InvalidProjectOutline Decode.Error
3435
| PackageInstallError PackageInstall.PackageInstallError
35-
| PackageFetchError Git.Error
36-
| GitError Git.Error
37-
| NoPackageVersions
3836
| NotAnApplication
3937

4038

@@ -156,17 +154,27 @@ clonePackage config packageName =
156154
FileSystem.makeTempDirectory config.fsPermission "gren-run"
157155
|> Task.mapError TempPathError
158156

159-
getVersion =
157+
clone tempDir =
160158
Git.fetchLatestVersion config.cpPermission packageName
161-
|> Task.mapError GitError
162-
163-
clone tempDir version =
164-
Git.clonePackage config.cpPermission tempDir packageName version
165-
|> Task.mapError PackageFetchError
159+
|> Task.andThen
160+
(\version ->
161+
Git.clonePackage
162+
config.cpPermission
163+
tempDir
164+
packageName
165+
version
166+
)
167+
|> Task.mapError
168+
(\e ->
169+
PackageInstallError <|
170+
PackageInstall.PackageInstallGitError <|
171+
{ package = packageName
172+
, error = e
173+
}
174+
)
166175
in
167176
Task.await getTempDir <| \tempDir ->
168-
Task.await getVersion <| \version ->
169-
Task.await (clone tempDir version) <| \_ ->
177+
Task.await (clone tempDir) <| \_ ->
170178
Task.await (getProjectOutline config.fsPermission tempDir) <| \outline ->
171179
Task.succeed
172180
{ projectPath = tempDir
@@ -230,15 +238,16 @@ getProjectOutline fsPermission path =
230238
path
231239
|> Path.append (Path.fromPosixString "gren.json")
232240
|> PackageInstall.readOutline fsPermission
233-
|> Task.mapError ReadOutlineError
241+
|> Task.mapError (PackageInstall.ReadProjectOutlineNoProject >> ReadProjectOutlineError)
234242
|> Task.andThen
235243
(\decodeResult ->
236244
when decodeResult is
237245
Ok outline ->
238246
Task.succeed outline
239247
Err err ->
240248
err
241-
|> InvalidProjectOutline
249+
|> PackageInstall.ReadProjectOutlineInvalidGrenJson
250+
|> ReadProjectOutlineError
242251
|> Task.fail
243252
)
244253

@@ -268,3 +277,37 @@ getOutputPath projectOutline =
268277
Just (getPath Backend.Html "app.html")
269278
Platform.Common ->
270279
Nothing
280+
281+
282+
prettifyError : Error -> PP.Document
283+
prettifyError error =
284+
when error is
285+
TempPathError err ->
286+
Help.report
287+
"FILESYSTEM ERROR"
288+
Nothing
289+
(PP.verticalBlock
290+
[ PP.words "Failed to access temporary file path."
291+
, PP.empty
292+
, PP.words "The error provided by the operating system is:"
293+
, PP.empty
294+
, PP.words (FileSystem.errorToString err)
295+
|> PP.color PP.Red
296+
|> PP.indent
297+
]
298+
)
299+
300+
NotAnApplication ->
301+
Help.report
302+
"NOT AN APPLICATION"
303+
Nothing
304+
( PP.words
305+
"The module you are trying to run is not an application."
306+
)
307+
308+
ReadProjectOutlineError err ->
309+
PackageInstall.prettifyProjectOutlineError err
310+
311+
PackageInstallError err ->
312+
PackageInstall.prettifyError err
313+

0 commit comments

Comments
 (0)