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

feat: add /pallets/errors #1176

Merged
merged 15 commits into from
Jan 23, 2023
164 changes: 164 additions & 0 deletions docs/src/openapi-v1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -962,6 +962,114 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/Error'
/pallets/{palletId}/errors:
marshacb marked this conversation as resolved.
Show resolved Hide resolved
get:
tags:
- pallets
summary: Get a list of errors for a pallet.
description: Returns a list of error item metadata for error items of the
specified palletId.
parameters:
- name: palletId
in: path
description: 'Name or index of the pallet to read error metadata for. Note: the pallet name must match what is
marshacb marked this conversation as resolved.
Show resolved Hide resolved
specified in the runtime metadata.'
required: true
schema:
type: string
- name: onlyIds
in: query
description: Only return the names (IDs) of the error items instead of all of each error
marshacb marked this conversation as resolved.
Show resolved Hide resolved
item's metadata.
required: false
schema:
type: boolean
- name: at
in: query
description: Block at which to retrieve a list of
the pallet's error items.
required: false
schema:
type: string
description: Block identifier, as the block height or block hash.
format: unsignedInteger or $hex
responses:
"200":
description: successful operation
content:
application/json:
schema:
type: array
description: Pallet info and Array of errorItemIds.
items:
$ref: '#/components/schemas/PalletErrors'
"400":
description: invalid blockId supplied for at query param
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
"404":
description: could not find pallet with palletId
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
/pallets/{palletId}/errors/{errorItemId}:
get:
tags:
- pallets
summary: Get the value of an error item.
description: Returns the value stored under the errorItemId.
parameters:
- name: palletId
in: path
description: 'Name or index of the pallet to read error metadata for. Note: the pallet name must match what is
TarikGul marked this conversation as resolved.
Show resolved Hide resolved
specified in the runtime metadata.'
required: true
schema:
type: string
- name: errorItemId
in: path
description: Id of the error item to query for.
required: true
schema:
type: string
- name: at
in: query
description: Block at which to query the error item at.
required: false
schema:
type: string
description: Block identifier, as the block height or block hash.
format: unsignedInteger or $hex
- name: metadata
in: query
description: Include the error items metadata (including documentation)
if set to true.
required: false
schema:
default: false
type: boolean
responses:
"200":
description: successful operation
content:
application/json:
schema:
$ref: '#/components/schemas/PalletErrorItem'
"400":
description: invalid blockId supplied for at query param
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
"404":
description: could not find resource with with id
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
/runtime/metadata:
get:
tags:
Expand Down Expand Up @@ -2277,6 +2385,62 @@ components:
$ref: '#/components/schemas/AssetInfo'
assetMetadata:
$ref: '#/components/schemas/AssetMetadata'
PalletErrors:
type: object
properties:
at:
$ref: '#/components/schemas/BlockIdentifiers'
pallet:
type: string
description: Name of the pallet.
example: "democracy"
palletIndex:
type: string
description: Index of the pallet for looking up errors.
example: "14"
items:
type: array
items:
$ref: '#/components/schemas/PalletErrorItemMetadata'
description: Array containing metadata for each error entry of the pallet.
PalletErrorsItem:
type: object
properties:
pallet:
type: string
description: Name of the pallet.
example: "democracy"
palletIndex:
type: string
description: Index of the pallet for looking up errors.
example: "14"
errorItem:
type: string
description: Name of the error item.
example: "ValueLow"
metadata:
$ref: '#/components/schemas/PalletErrorItemMetadata'
PalletErrorItemMetadata:
type: object
properties:
name:
type: string
example: "InsufficientFunds"
description: The error item's name (which is the same as the error item's ID).
fields:
type: array
example: "[]"
index:
type: string
example: "0"
description: The index of the error item in the lists of pallet errors
docs:
type: string
example: " Information concerning any given error.\n\n TWOX-NOTE: SAFE as indexes are not under an attacker’s control."
args:
type: array
example: "[]"
description: Metadata of an error item from a FRAME pallet.
PalletStorage:
type: object
properties:
Expand Down
1 change: 1 addition & 0 deletions src/chains-config/defaultControllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export const defaultControllers: ControllerConfig = {
'NodeTransactionPool',
'NodeVersion',
'PalletsAssets',
'PalletsErrors',
'PalletsStakingProgress',
'PalletsStorage',
'Paras',
Expand Down
1 change: 1 addition & 0 deletions src/chains-config/polkadotControllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export const polkadotControllers: ControllerConfig = {
'NodeNetwork',
'NodeTransactionPool',
'NodeVersion',
'PalletsErrors',
'PalletsStakingProgress',
'PalletsStorage',
'Paras',
Expand Down
1 change: 1 addition & 0 deletions src/chains-config/westendControllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export const westendControllers: ControllerConfig = {
'NodeNetwork',
'NodeTransactionPool',
'NodeVersion',
'PalletsErrors',
TarikGul marked this conversation as resolved.
Show resolved Hide resolved
'PalletsStakingProgress',
'PalletsStorage',
'Paras',
Expand Down
2 changes: 2 additions & 0 deletions src/controllers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { ContractsInk } from './contracts';
import { NodeNetwork, NodeTransactionPool, NodeVersion } from './node';
import {
PalletsAssets,
PalletsErrors,
PalletsStakingProgress,
PalletsStorage,
} from './pallets';
Expand Down Expand Up @@ -56,6 +57,7 @@ export const controllers = {
AccountsStakingPayouts,
ContractsInk,
PalletsAssets,
PalletsErrors,
PalletsStakingProgress,
PalletsStorage,
NodeNetwork,
Expand Down
88 changes: 88 additions & 0 deletions src/controllers/pallets/PalletsErrorsController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// Copyright 2017-2022 Parity Technologies (UK) Ltd.
// This file is part of Substrate API Sidecar.
//
// Substrate API Sidecar is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

import { ApiPromise } from '@polkadot/api';
import { stringCamelCase } from '@polkadot/util';
import { RequestHandler } from 'express-serve-static-core';

import { PalletsErrorService } from '../../services';
import { IPalletsErrorsParam } from '../../types/requests';
import AbstractController from '../AbstractController';

/**
* `/pallets/{palletId}/errors`
*
* Returns the metadata for each error item of the pallet.
*
* `/pallets/{palletId}/errors/{errorItemId}`
*
* Returns the info for the errorItemId.
*
* See `docs/src/openapi-v1.yaml` for usage information.
*/
export default class PalletsErrorsController extends AbstractController<PalletsErrorService> {
constructor(api: ApiPromise) {
super(api, '/pallets/:palletId/errors', new PalletsErrorService(api));

this.initRoutes();
}

protected initRoutes(): void {
this.safeMountAsyncGetHandlers([
['/:errorItemId', this.getErrorById as RequestHandler],
['/', this.getErrors],
]);
}

private getErrorById: RequestHandler<IPalletsErrorsParam, unknown, unknown> =
async (
{ query: { at, metadata }, params: { palletId, errorItemId } },
res
): Promise<void> => {
const metadataArg = metadata === 'true';
const hash = await this.getHashFromAt(at);
const historicApi = await this.api.at(hash);

PalletsErrorsController.sanitizedSend(
res,
await this.service.fetchErrorItem(historicApi, {
hash,
// stringCamelCase ensures we don't have snake case or kebab case
palletId: stringCamelCase(palletId),
errorItemId: stringCamelCase(errorItemId),
metadata: metadataArg,
})
);
};

private getErrors: RequestHandler = async (
{ params: { palletId }, query: { at, onlyIds } },
res
): Promise<void> => {
const onlyIdsArg = onlyIds === 'true';
const hash = await this.getHashFromAt(at);
const historicApi = await this.api.at(hash);

PalletsErrorsController.sanitizedSend(
res,
await this.service.fetchErrors(historicApi, {
hash,
palletId: stringCamelCase(palletId),
onlyIds: onlyIdsArg,
})
);
};
}
1 change: 1 addition & 0 deletions src/controllers/pallets/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>.

export { default as PalletsAssets } from './PalletsAssetsController';
export { default as PalletsErrors } from './PalletsErrorsController';
export { default as PalletsStakingProgress } from './PalletsStakingProgressController';
export { default as PalletsStorage } from './PalletsStorageController';
Loading