diff --git a/src/Rendering/JSON.hs b/src/Rendering/JSON.hs index e4ced5665..0a988c12b 100644 --- a/src/Rendering/JSON.hs +++ b/src/Rendering/JSON.hs @@ -8,6 +8,7 @@ module Rendering.JSON , renderJSONAST , renderSymbolTerms , renderJSONError +, renderJSONSymbolError , renderJSONDiffError , SomeJSON(..) ) where @@ -94,12 +95,22 @@ instance ToJSON a => ToJSON (JSONAST a) where renderSymbolTerms :: ToJSON a => [a] -> JSON "files" SomeJSON renderSymbolTerms = JSON . map SomeJSON -renderJSONError :: Blob -> String -> JSON "trees" SomeJSON -renderJSONError Blob{..} e = JSON [ SomeJSON (object [ "error" .= err ]) ] - where err = object [ "message" .= e - , "path" .= blobPath - , "language" .= blobLanguage ] +-- | Render an error for symbols. +renderJSONSymbolError :: Blob -> String -> JSON "files" SomeJSON +renderJSONSymbolError blob e = JSON [ renderError blob e ] +-- | Render an error for terms. +renderJSONError :: Blob -> String -> JSON "trees" SomeJSON +renderJSONError blob e = JSON [ renderError blob e ] + +-- | Render an error for a particular blob. +renderError :: ToJSON a => Blob -> a -> SomeJSON +renderError Blob{..} e = SomeJSON $ object + [ "error" .= e + , "path" .= blobPath + , "language" .= blobLanguage ] + +-- | Render an error for diffs. renderJSONDiffError :: BlobPair -> String -> JSON "diffs" SomeJSON renderJSONDiffError pair e = JSON [ SomeJSON (object [ "error" .= err ]) ] where err = object ["message" .= e, "stat" .= toJSON (JSONStat pair)] diff --git a/src/Rendering/Renderer.hs b/src/Rendering/Renderer.hs index befc59c39..388deceea 100644 --- a/src/Rendering/Renderer.hs +++ b/src/Rendering/Renderer.hs @@ -14,6 +14,7 @@ module Rendering.Renderer , renderToSymbols , renderTreeGraph , renderJSONError +, renderJSONSymbolError , renderJSONDiffError , renderJSONSummaryError , Summaries(..) diff --git a/src/Semantic/Parse.hs b/src/Semantic/Parse.hs index fb705abdf..1bd12cc31 100644 --- a/src/Semantic/Parse.hs +++ b/src/Semantic/Parse.hs @@ -33,7 +33,7 @@ runParse JSONGraphTermRenderer = withParsedBlobs' renderJSONError (render renderAdjGraph blob term = renderJSONAdjTerm blob (renderTreeGraph term) runParse SExpressionTermRenderer = withParsedBlobs (const (serialize (SExpression ByConstructorName))) runParse ShowTermRenderer = withParsedBlobs (const (serialize Show . quieterm)) -runParse (SymbolsTermRenderer fields) = withParsedBlobs (\ blob -> render (renderSymbolTerms . renderToSymbols fields blob)) >=> serialize JSON +runParse (SymbolsTermRenderer fields) = withParsedBlobs' renderJSONSymbolError (\ blob -> render (renderSymbolTerms . renderToSymbols fields blob)) >=> serialize JSON runParse DOTTermRenderer = withParsedBlobs (const (render renderTreeGraph)) >=> serialize (DOT (termStyle "terms")) runParse QuietTermRenderer = distributeFoldMap $ \blob -> showTiming blob <$> time' ((parseSomeBlob blob >>= withSomeTerm (fmap (const (Right ())) . serialize Show . quieterm)) `catchError` \(SomeException e) -> pure (Left (show e))) diff --git a/test/Semantic/Spec.hs b/test/Semantic/Spec.hs index f53cb9019..e30c17fea 100644 --- a/test/Semantic/Spec.hs +++ b/test/Semantic/Spec.hs @@ -13,7 +13,7 @@ spec = parallel $ do describe "parseBlob" $ do it "returns error if given an unknown language (json)" $ do output <- fmap runBuilder . runTask $ runParse JSONTermRenderer [ methodsBlob { blobLanguage = Unknown } ] - output `shouldBe` "{\"trees\":[{\"error\":{\"path\":\"methods.rb\",\"language\":\"Unknown\",\"message\":\"NoLanguageForBlob \\\"methods.rb\\\"\"}}]}\n" + output `shouldBe` "{\"trees\":[{\"path\":\"methods.rb\",\"error\":\"NoLanguageForBlob \\\"methods.rb\\\"\",\"language\":\"Unknown\"}]}\n" it "throws if given an unknown language for sexpression output" $ do runTask (runParse SExpressionTermRenderer [methodsBlob { blobLanguage = Unknown }]) `shouldThrow` (== ExitFailure 1)