Add location to thrown exception when parsing YAML

Signed-off-by: Fredrik Adelöw <freben@gmail.com>
This commit is contained in:
Fredrik Adelöw
2021-03-16 15:57:02 +01:00
parent 6d2a583162
commit d2f4efc5d5
3 changed files with 17 additions and 5 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-catalog-backend': patch
---
Add location to thrown exception when parsing YAML
@@ -159,7 +159,7 @@ describe('parseEntityYaml', () => {
expect(results).toEqual([
result.generalError(
testLoc,
'YAML error, YAMLSemanticError: Plain value cannot start with reserved character `',
'YAML error at my-loc-type:my-loc-target, YAMLSemanticError: Plain value cannot start with reserved character `',
),
]);
});
@@ -198,7 +198,7 @@ describe('parseEntityYaml', () => {
}),
result.generalError(
testLoc,
'YAML error, YAMLSemanticError: Nested mappings are not allowed in compact mappings',
'YAML error at my-loc-type:my-loc-target, YAMLSemanticError: Nested mappings are not allowed in compact mappings',
),
]);
});
@@ -14,7 +14,11 @@
* limitations under the License.
*/
import { Entity, LocationSpec } from '@backstage/catalog-model';
import {
Entity,
LocationSpec,
stringifyLocationReference,
} from '@backstage/catalog-model';
import lodash from 'lodash';
import yaml from 'yaml';
import * as result from '../results';
@@ -28,13 +32,16 @@ export function* parseEntityYaml(
try {
documents = yaml.parseAllDocuments(data.toString('utf8')).filter(d => d);
} catch (e) {
yield result.generalError(location, `Failed to parse YAML, ${e}`);
const loc = stringifyLocationReference(location);
const message = `Failed to parse YAML at ${loc}, ${e}`;
yield result.generalError(location, message);
return;
}
for (const document of documents) {
if (document.errors?.length) {
const message = `YAML error, ${document.errors[0]}`;
const loc = stringifyLocationReference(location);
const message = `YAML error at ${loc}, ${document.errors[0]}`;
yield result.generalError(location, message);
} else {
const json = document.toJSON();