Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handled the document object being undefined in the parser result. #912

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions library/src/containers/Error/Error.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ const renderErrors = (errors: ValidationError[]): React.ReactNode => {

return errors
.map((singleError: ValidationError, index: number) => {
if (!singleError || !singleError.title || !singleError.location) {
if (!singleError) {
return null;
}
return (
<div key={index} className="flex">
<span>{`${singleError.location.startLine}.`}</span>
<code className="whitespace-pre-wrap break-all ml-2">
{singleError.title}
</code>
Expand All @@ -39,7 +38,7 @@ export const Error: React.FunctionComponent<Props> = ({ error }) => {
<div className="panel-item">
<div className="panel-item--center p-8">
<section className="shadow rounded bg-gray-200 border-red-500 border-l-8">
<h2 className="p-2">
<h2 className="bg-gray-600">
{title ? `${ERROR_TEXT}: ${title}` : ERROR_TEXT}
</h2>
{validationErrors && validationErrors.length ? (
Expand Down
30 changes: 27 additions & 3 deletions library/src/helpers/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ import { OpenAPISchemaParser } from '@asyncapi/openapi-schema-parser';
import { ProtoBuffSchemaParser } from '@asyncapi/protobuf-schema-parser';
import { AvroSchemaParser } from '@asyncapi/avro-schema-parser';

import { ErrorObject, ParserReturn, FetchingSchemaInterface } from '../types';
import {
ErrorObject,
ParserReturn,
FetchingSchemaInterface,
ValidationError,
} from '../types';

import { VALIDATION_ERRORS_TYPE } from '../constants';

Expand All @@ -18,8 +23,27 @@ export class Parser {
parserOptions?: any,
): Promise<ParserReturn> {
try {
const { document } = await asyncapiParser.parse(content, parserOptions);
return { asyncapi: document };
const parseResult = await asyncapiParser.parse(content, parserOptions);

let error: {
title: string | undefined;
validationErrors: ValidationError[] | undefined;
} = {
title: 'There were errors validating Asyncapi document',
validationErrors: undefined,
};

if (parseResult.document === undefined) {
parseResult.diagnostics.forEach(diagnostic => {
if (diagnostic.code.toString().includes('error')) {
const tempObj = { title: diagnostic.message };
error.validationErrors = [(tempObj as unknown) as ValidationError];
}
});
throw error;
}

return { asyncapi: parseResult.document };
} catch (err) {
return this.handleError(err as ErrorObject);
}
Expand Down
Loading