Add errors to documentation (#1841)

* Add errors to docs

* Fix broken anchor links (camel-case links but lowercase anchors)
This commit is contained in:
Axel Chalon
2020-02-13 23:01:05 +01:00
committed by GitHub
parent 41764562e1
commit 8210c708ac
2 changed files with 39 additions and 2 deletions
+37 -1
View File
@@ -21,13 +21,14 @@ const STATIC_TEXT = '\n\n(NOTE: These were generated from a static/snapshot view
const DESC_CONSTANTS = `\n\nThe following sections contain the module constants, also known as parameter types. These can only be changed as part of a runtime upgrade. On the api, these are exposed via \`api.consts.<module>.<method>\`. ${STATIC_TEXT}\n`;
const DESC_EXTRINSICS = `\n\nThe following sections contain Extrinsics methods are part of the default Substrate runtime. On the api, these are exposed via \`api.tx.<module>.<method>\`. ${STATIC_TEXT}\n`;
const DESC_ERRORS = `\n\nThis page lists the errors that can be encountered in the different modules. ${STATIC_TEXT}\n`;
const DESC_EVENTS = `\n\nEvents are emitted for certain operations on the runtime. The following sections describe the events that are part of the default Substrate runtime. ${STATIC_TEXT}\n`;
const DESC_RPC = '\n\nThe following sections contain RPC methods that are Remote Calls available by default and allow you to interact with the actual node, query, and submit.\n';
const DESC_STORAGE = `\n\nThe following sections contain Storage methods are part of the default Substrate runtime. On the api, these are exposed via \`api.query.<module>.<method>\`. ${STATIC_TEXT}\n`;
/** @internal */
function sectionLink (sectionName: string): string {
return `- **[${stringCamelCase(sectionName)}](#${stringCamelCase(sectionName)})**\n\n`;
return `- **[${stringCamelCase(sectionName)}](#${stringCamelCase(sectionName).toLowerCase()})**\n\n`;
}
/** @internal */
@@ -101,6 +102,35 @@ function addConstants (metadata: MetadataLatest): string {
return renderHeading + renderAnchors + sections;
}
/** @internal */
function addErrors (metadata: MetadataLatest): string {
const renderHeading = `## ${ANCHOR_TOP}Errors${DESC_ERRORS}`;
const orderedSections = metadata.modules.sort(sortByName);
let renderAnchors = '';
const sections = orderedSections.reduce((md, moduleMetadata): string => {
if (moduleMetadata.errors.isEmpty) {
return md;
}
const sectionName = stringLowerFirst(moduleMetadata.name.toString());
renderAnchors += sectionLink(sectionName);
const renderSection = generateSectionHeader(md, sectionName);
const orderedErrors = moduleMetadata.errors.sort(sortByName);
return orderedErrors.reduce((md, error): string => {
const errorName = error.name.toString();
const doc = error.documentation.reduce((md, doc): string =>
`${md.length ? `${md} ` : ''}${doc.trim()}`, '');
return `${md}\n### ${errorName}` + `${doc ? `\n- **summary**: ${doc}\n` : '\n'}`;
}, renderSection);
}, '');
return renderHeading + renderAnchors + sectionLink('substrate') + sections;
}
/** @internal */
function addEvents (metadata: MetadataLatest): string {
const renderHeading = `## ${ANCHOR_TOP}Events${DESC_EVENTS}`;
@@ -248,6 +278,11 @@ function writeToEventsMd (metadata: MetadataLatest): void {
writeFile('docs/substrate/events.md', addEvents(metadata));
}
/** @internal */
function writeToErrorsMd (metadata: MetadataLatest): void {
writeFile('docs/substrate/errors.md', addErrors(metadata));
}
const registry = new TypeRegistry();
const decorated = new Decorated(registry, rpcdata);
const latest = decorated.metadata.asLatest;
@@ -257,3 +292,4 @@ writeToConstantsMd(latest);
writeToStorageMd(latest);
writeToExtrinsicsMd(latest);
writeToEventsMd(latest);
writeToErrorsMd(latest);