Skip to content

Commit

Permalink
fix: do not panic, return the error instead (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
JounQin committed Jun 27, 2022
1 parent f22ab75 commit fb92edf
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/itchy-snails-exist.md
@@ -0,0 +1,5 @@
---
"sh-syntax": patch
---

fix: do not panic, return the error instead
4 changes: 1 addition & 3 deletions main.go
Expand Up @@ -5,7 +5,6 @@ package main

import (
"container/list"
"fmt"

"github.com/mailru/easyjson"
"github.com/rx-ts/sh-syntax/processor"
Expand Down Expand Up @@ -117,8 +116,7 @@ func process(
bytes, err := easyjson.Marshal(&result)

if err != nil {
fmt.Println(err)
panic(err)
bytes = []byte(err.Error())
}

return &bytes[0]
Expand Down
Binary file modified main.wasm
Binary file not shown.
15 changes: 12 additions & 3 deletions src/processor.ts
@@ -1,10 +1,10 @@
import { IParseError, File, ShOptions, LangVariant } from './types.js'

export class ParseError extends Error implements IParseError {
Filename: string
Filename?: string
Incomplete: boolean
Text: string
Pos: {
Pos?: {
Col: number
Line: number
Offset: number
Expand Down Expand Up @@ -39,7 +39,7 @@ export const getProcessor = (
originalText: string
},
): Promise<string>

// eslint-disable-next-line sonarjs/cognitive-complexity
async function processor(
textOrAst: File | string,
{
Expand Down Expand Up @@ -168,6 +168,15 @@ export const getProcessor = (

const string = decoder.decode(result.subarray(0, end))

// naive check whether the string is a json
if (!string.startsWith('{"') && !string.endsWith('}')) {
throw new ParseError({
Filename: filepath,
Incomplete: true,
Text: string,
})
}

const {
file,
text: processedText,
Expand Down
4 changes: 2 additions & 2 deletions src/types.ts
Expand Up @@ -85,8 +85,8 @@ export interface File extends Node {
}

export interface IParseError {
Filename: string
Filename?: string
Incomplete: boolean
Text: string
Pos: Pos
Pos?: Pos
}

0 comments on commit fb92edf

Please sign in to comment.