-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Surface errors appearing within Flux tables
- Loading branch information
Showing
6 changed files
with
97 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import {checkQueryResult} from 'src/shared/utils/checkQueryResult' | ||
|
||
describe('checkQueryResult', () => { | ||
test('throws an error when the response has an error table', () => { | ||
const RESPONSE = `#group,true,true | ||
#datatype,string,string | ||
#default,, | ||
,error,reference | ||
,"function references unknown column ""_value""",` | ||
|
||
expect(() => { | ||
checkQueryResult(RESPONSE) | ||
}).toThrow('function references unknown column') | ||
}) | ||
|
||
test('does not throw an error when the response is valid', () => { | ||
const RESPONSE = `#group,false,false,true,true,false,false,true,true,true | ||
#datatype,string,long,dateTime:RFC3339,dateTime:RFC3339,dateTime:RFC3339,long,string,string,string | ||
#default,_result,,,,,,,, | ||
,result,table,_start,_stop,_time,_value,_measurement,host,_field | ||
,,0,2019-03-21T18:54:14.113478Z,2019-03-21T19:54:14.113478Z,2019-03-21T18:54:21Z,4780101632,mem,oox4k.local,active | ||
,,0,2019-03-21T18:54:14.113478Z,2019-03-21T19:54:14.113478Z,2019-03-21T18:54:31Z,5095436288,mem,oox4k.local,active` | ||
|
||
expect(() => { | ||
checkQueryResult(RESPONSE) | ||
}).not.toThrow() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
Given Flux query response as a CSV, check if the CSV contains an error table | ||
as the first result. If it does, throw the error message contained within | ||
that table. | ||
For example, given the following response: | ||
#datatype,string,long | ||
,error,reference | ||
,Failed to parse query,897 | ||
we want to throw an error with the message "Failed to parse query". | ||
See https://github.com/influxdata/flux/blob/master/docs/SPEC.md#errors. | ||
*/ | ||
export const checkQueryResult = (file: string): void => { | ||
// Don't check the whole file, since it could be huge and the error table | ||
// will be within the first few lines (if it exists) | ||
const fileHead = file.slice(0, findNthIndex(file, '\n', 6)) | ||
|
||
const lines = fileHead.split('\n').filter(line => !line.startsWith('#')) | ||
|
||
if (!lines.length || !lines[0].includes('error') || !lines[1]) { | ||
return | ||
} | ||
|
||
const header = lines[0].split(',').map(s => s.trim()) | ||
const row = lines[1].split(',').map(s => s.trim()) | ||
const index = header.indexOf('error') | ||
|
||
if (index === -1 || !row[index]) { | ||
return | ||
} | ||
|
||
// Trim off extra quotes at start and end of message | ||
const errorMessage = row[index].replace(/^"/, '').replace(/"$/, '') | ||
|
||
throw new Error(errorMessage) | ||
} | ||
|
||
const findNthIndex = (s: string, c: string, n: number) => { | ||
let count = 0 | ||
let i = 0 | ||
|
||
while (i < s.length) { | ||
if (s[i] == c) { | ||
count += 1 | ||
} | ||
|
||
if (count === n) { | ||
return i | ||
} | ||
|
||
i += 1 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters