Skip to content
This repository has been archived by the owner on Oct 1, 2018. It is now read-only.

Commit

Permalink
Informative warning & error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
zdne committed Jul 18, 2013
1 parent 73efa42 commit 8b64222
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 23 deletions.
4 changes: 2 additions & 2 deletions src/AssetParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ namespace snowcrash {
break;

default:
result.first.error = Error("unexpected block", BusinessError, cur->sourceMap);
result.first.error = UnexpectedBlockError(*cur);
break;
}

Expand Down Expand Up @@ -218,7 +218,7 @@ namespace snowcrash {

// WARN: Dangling block
std::stringstream ss;
ss << "dangling message-" << SectionName(originalSection);
ss << "dangling " << SectionName(originalSection);
ss << ", increase its indentation to nest it properly";
result.first.warnings.push_back(Warning(ss.str(),
FormattingWarning,
Expand Down
2 changes: 1 addition & 1 deletion src/BlueprintParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ namespace snowcrash {
break;

default:
result.first.error = Error("unexpected block", BusinessError, cur->sourceMap);
result.first.error = UnexpectedBlockError(*cur);
break;
}

Expand Down
38 changes: 36 additions & 2 deletions src/BlueprintParserCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#define SNOWCRASH_BLUEPRINTPARSERCORE_H

#include <algorithm>
#include <sstream>
#include "ParserCore.h"
#include "SourceAnnotation.h"
#include "MarkdownBlock.h"
Expand Down Expand Up @@ -57,10 +58,10 @@ namespace snowcrash {
return "response";

case BodySection:
return "body";
return "message-body";

case SchemaSection:
return "schema";
return "message-schema";

case HeadersSection:
return "headers";
Expand Down Expand Up @@ -281,6 +282,39 @@ namespace snowcrash {
result.error = Error("unexpected markdown closure", ApplicationError, parent->sourceMap);
return false;
}

/**
* \brief Construct an Unexpected block error.
* \param block A Markdown block that is unexpected.
* \return An Error with description relevant to the type of the unexpected block.
*/
FORCEINLINE Error UnexpectedBlockError(const MarkdownBlock& block) {
static const char *NotExpectedMessage = " is either not appropriate for the current context or its keyword has not been recognized, ";

std::stringstream ss;
ss << "unexpected block, ";

switch (block.type) {
case HeaderBlockType:
ss << "this Markdown header" << NotExpectedMessage;
ss << "recognized keywords include: Group, <HTTP method>, <URI template>, Object ...";
break;

case ListBlockBeginType:
case ListItemBlockBeginType:

ss << "this Markdown list item" << NotExpectedMessage;
ss << "recognized keywords include: Request, Response <HTTP status code>, Headers, Parameters ...";
break;


default:
ss << "a block of this type is not expected in the current context";
break;
}

return Error(ss.str(), BusinessError, block.sourceMap);
}
}

#endif
4 changes: 2 additions & 2 deletions src/HeaderParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ namespace snowcrash {
break;

default:
result.first.error = Error("unexpected block", BusinessError, cur->sourceMap);
result.first.error = UnexpectedBlockError(*cur);
break;
}

Expand Down Expand Up @@ -202,7 +202,7 @@ namespace snowcrash {
if (FindHeader(left.headers, *it) != left.headers.end()) {
// WARN: overshadowing header definition
std::stringstream ss;
ss << "overshadowing `" << it->first << "` header definition";
ss << "overshadowing previous `" << it->first << "` header definition";
result.warnings.push_back(Warning(ss.str(),
RedefinitionWarning,
rightSourceMap));
Expand Down
3 changes: 2 additions & 1 deletion src/ListUtility.h
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,8 @@ namespace snowcrash {
SourceData& data,
SourceDataBlock& sourceMap) {

static const std::string FormattingWarningMesssage = "content is expected to be preformatted code block";
static const std::string FormattingWarningMesssage = "content is expected to be a pre-formatted code block, "\
"separate it by an empty line and indent every of its line by at least 4 spaces or 1 tab";

ParseSectionResult result = std::make_pair(Result(), cur);
BlockIterator sectionCur = cur;
Expand Down
8 changes: 4 additions & 4 deletions src/MethodParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,13 @@ namespace snowcrash {

case ObjectSection:
// ERR: Unexpected object definition
result.first.error = Error("unexpected object definiton, object can be only defined in a resource section",
result.first.error = Error("unexpected object definiton, an object can be only defined in the resource section",
SymbolError,
cur->sourceMap);
break;

default:
result.first.error = Error("unexpected block", BusinessError, cur->sourceMap);
result.first.error = UnexpectedBlockError(*cur);
break;
}

Expand Down Expand Up @@ -294,7 +294,7 @@ namespace snowcrash {
else if (!payload.body.empty()) {
// WARN: not empty body
std::stringstream ss;
ss << "the " << code << " response MUST NOT include a message-body";
ss << "the " << code << " response MUST NOT include a " << SectionName(BodySection);
result.warnings.push_back(Warning(ss.str(),
EmptyDefinitionWarning,
sourceMap));
Expand All @@ -306,7 +306,7 @@ namespace snowcrash {
if (warnEmptyBody) {
// WARN: empty body
std::stringstream ss;
ss << "empty " << SectionName(section) << " message-body";
ss << "empty " << SectionName(section) << " " << SectionName(BodySection);
result.warnings.push_back(Warning(ss.str(),
EmptyDefinitionWarning,
sourceMap));
Expand Down
4 changes: 2 additions & 2 deletions src/PayloadParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ namespace snowcrash {
break;

default:
result.first.error = Error("unexpected block", BusinessError, cur->sourceMap);
result.first.error = UnexpectedBlockError(*cur);
break;
}

Expand Down Expand Up @@ -330,7 +330,7 @@ namespace snowcrash {
// WARN: asset already set
BlockIterator nameBlock = ListItemNameBlock(begin, end);
std::stringstream ss;
ss << "ignoring " << SectionName(section) << " asset, asset already defined";
ss << "ignoring additional " << SectionName(section) << " content, content is already defined";
result.first.warnings.push_back(Warning(ss.str(),
RedefinitionWarning,
nameBlock->sourceMap));
Expand Down
6 changes: 3 additions & 3 deletions src/ResourceGroupParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ namespace snowcrash {
break;

default:
result.first.error = Error("unexpected block", BusinessError, cur->sourceMap);
result.first.error = UnexpectedBlockError(*cur);
break;
}

Expand Down Expand Up @@ -164,8 +164,8 @@ namespace snowcrash {
if (duplicate != group.resources.end() ||
globalDuplicate.first != parser.blueprint.resourceGroups.end()) {

// WARN: duplicate resource
result.first.warnings.push_back(Warning("resource `" +
// WARN: Duplicate resource
result.first.warnings.push_back(Warning("the resource `" +
resource.uriTemplate +
"` is already defined",
DuplicateWarning,
Expand Down
15 changes: 9 additions & 6 deletions src/ResourceParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ namespace snowcrash {
break;

case UndefinedSection:
CheckAmbiguousMethod(cur, bounds.second, result.first);
CheckAmbiguousMethod(cur, bounds.second, resource, result.first);
result.second = CloseListItemBlock(cur, bounds.second);
break;

Expand All @@ -193,7 +193,7 @@ namespace snowcrash {
break;

default:
result.first.error = Error("unexpected block", BusinessError, cur->sourceMap);
result.first.error = UnexpectedBlockError(*cur);
break;
}

Expand Down Expand Up @@ -253,7 +253,7 @@ namespace snowcrash {
return result;

if (!resource.object.name.empty()) {
// WARN: object already defined
// WARN: Object already defined
std::stringstream ss;
ss << "ignoring additional object definiton for `";
if (!resource.name.empty()) {
Expand All @@ -262,7 +262,7 @@ namespace snowcrash {
else {
ss << resource.uriTemplate;
}
ss << "` resource, a resource can be represented single (1) object only";
ss << "` resource, a resource can be represented by a single object only";

BlockIterator nameBlock = ListItemNameBlock(begin, end);
result.first.warnings.push_back(Warning(ss.str(),
Expand All @@ -274,7 +274,7 @@ namespace snowcrash {

ResourceObjectSymbolTable::const_iterator it = parser.symbolTable.resourceObjects.find(payload.name);
if (it != parser.symbolTable.resourceObjects.end()) {
// ERR: symbol already defined
// ERR: Symbol already defined
std::stringstream ss;
ss << "symbol `" << payload.name << "` already defined";
BlockIterator nameBlock = ListItemNameBlock(begin, end);
Expand Down Expand Up @@ -381,6 +381,7 @@ namespace snowcrash {
// method header -> implies possible additional method intended
static void CheckAmbiguousMethod(const BlockIterator& begin,
const BlockIterator& end,
const Resource& resource,
Result& result) {

if (begin == end ||
Expand All @@ -394,7 +395,9 @@ namespace snowcrash {
methodSignature == NamedMethodSignature) {
// WARN: ignoring possible method header
std::stringstream ss;
ss << "ambiguous method `" << begin->content << "`, check previous resource definition";
ss << "unexpected method `" << begin->content << "`, ";
ss << "to the define muliple methods for the `" << resource.uriTemplate << "` resource remove the method from its definition, ";
ss << "e.g. `# " << resource.uriTemplate << "`";
result.warnings.push_back(Warning(ss.str(), IgnoringWarning, begin->sourceMap));
}
}
Expand Down

0 comments on commit 8b64222

Please sign in to comment.