apla-blockchain-tools
Version:
Module contains a number of tools to work with Apla Blockchain
382 lines (307 loc) • 10.7 kB
JavaScript
;
// MIT License
//
// Copyright (c) 2016-2018 AplaProject
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
/*
* Package: apla-blockchain-tools
* Author: Anton Zuev
* mail: a.zuev@apla.io
* Company: apla.io
*/
/****************** AplaAPI Errors description ******************/
/**
* checks whether any error happen during method execution
* @param json - response from the node
* @return {AplaAPIError | *}
*/
module.exports.getError = (json) => {
if(!json.error){
return null;
}
switch (json.error){
case "E_DBNIL":
return new DBNilError(json);
case "E_KEYNOTFOUND":
return new KeyNotFoundError(json);
case "E_ECOSYSTEM":
return new EcosystemError(json);
case "E_CONTRACT":
return new ContractNotFoundError(json);
case "E_HASHNOTFOUND":
return new HashNotFoundError(json);
case "E_HASHWRONG":
return new WrongHashError(json);
case "E_EMPTYSIGN":
return new EmptySignatureError(json);
case "E_EMPTYPUBLIC":
return new EmptyPublicKeyError(json);
case "E_REFRESHTOKEN":
return new RefreshTokenError(json);
case "E_RECOVERED":
return new RecoveredError(json);
case "E_QUERY":
return new DBQueryError(json);
case "E_NOTINSTALLED":
return new NotInstalledError(json);
case "E_INVALIDWALLET":
return new InvalidWalletError(json);
case "E_INSTALLED":
return new AlreadyInstalledError(json);
case "E_VDECREATED":
return new VDECreatedError(json);
case "E_VDE":
return new VDEError(json);
case "E_UNKNOWNUID":
return new UnknownUIDError(json);
case "E_UNDEFINEVAL":
return new UndefinedValueError(json);
case "E_UNAUTHORIZED":
return new UnauthorizedError(json);
case "E_TOKENEXPIRED":
return new TokenExpiredError(json);
case "E_TOKEN":
return new TokenError(json);
case "E_TABLENOTFOUND":
return new TableNotFoundError(json);
case "E_STATELOGIN":
return new LoginStateError(json);
case "E_SIGNATURE":
return new SignatureError(json);
case "E_SERVER":
return new ServerError(json);
default:
return new AplaAPIError(json.msg, json.error);
}
};
class AplaAPIError extends Error {
constructor (message, params, type) {
// Calling parent constructor of base Error class.
super(message);
this.name = this.constructor.name;
this.params = params;
this.type = type;
Error.captureStackTrace(this, this.constructor);
}
}
module.exports.AplaAPIError = AplaAPIError;
class KeyNotFoundError extends AplaAPIError{
constructor(options){
super(options.msg, null, options.error);
}
}
module.exports.KeyNotFoundError = KeyNotFoundError;
class ContractNotFoundError extends AplaAPIError{
constructor(options){
super(options.msg, options.params, options.error);
}
}
module.exports.ContractNotFoundError = ContractNotFoundError;
class DBNilError extends AplaAPIError{
constructor(options){
super(options.msg, options.params, options.error);
}
}
module.exports.DBNilError = DBNilError;
class EcosystemError extends AplaAPIError{
constructor(options){
super(options.msg, options.params, options.error);
}
}
module.exports.EcosystemError = EcosystemError;
class EmptyPublicKeyError extends AplaAPIError{
constructor(options){
super(options.msg, options.params, options.error);
}
}
module.exports.EmptyPublicKeyError = EmptyPublicKeyError;
class EmptySignatureError extends AplaAPIError{
constructor(options){
super(options.msg, options.params, options.error);
}
}
module.exports.EmptySignatureError = EmptySignatureError;
class WrongHashError extends AplaAPIError{
constructor(options){
super(options.msg, options.params, options.error);
}
}
module.exports.WrongHashError = WrongHashError;
class HashNotFoundError extends AplaAPIError{
constructor(options){
super(options.msg, options.params, options.error);
}
}
module.exports.HashNotFoundError = HashNotFoundError;
class AlreadyInstalledError extends AplaAPIError{
constructor(options){
super(options.msg, options.params, options.error);
}
}
module.exports.AlreadyInstalledError = AlreadyInstalledError;
class NotInstalledError extends AplaAPIError{
constructor(options){
super(options.msg, options.params, options.error);
}
}
module.exports.NotInstalledError = NotInstalledError;
class DBQueryError extends AplaAPIError{
constructor(options){
super(options.msg, options.params, options.error);
}
}
module.exports.DBQueryError = DBQueryError;
class RecoveredError extends AplaAPIError{
constructor(options){
super(options.msg, options.params, options.error);
}
}
module.exports.RecoveredError = RecoveredError;
class RefreshTokenError extends AplaAPIError{
constructor(options){
super(options.msg, options.params, options.error);
}
}
module.exports.RefreshTokenError = RefreshTokenError;
class ServerError extends AplaAPIError{
constructor(options){
super(options.msg, options.params, options.error);
}
}
module.exports.ServerError = ServerError;
class SignatureError extends AplaAPIError{
constructor(options){
super(options.msg, options.params, options.error);
}
}
module.exports.SignatureError = SignatureError;
// member is not a member of ecosystem
class LoginStateError extends AplaAPIError{
constructor(options){
super(options.msg, options.params, options.error);
}
}
module.exports.LoginStateError = LoginStateError;
class TableNotFoundError extends AplaAPIError{
constructor(options){
super(options.msg, options.params, options.error);
}
}
module.exports.TableNotFoundError = TableNotFoundError;
class TokenError extends AplaAPIError{
constructor(options){
super(options.msg, options.params, options.error);
}
}
module.exports.TokenError = TokenError;
class TokenExpiredError extends AplaAPIError{
constructor(options){
super(options.msg, options.params, options.error);
}
}
module.exports.TokenExpiredError = TokenExpiredError;
class UnauthorizedError extends AplaAPIError{
constructor(options){
super(options.msg, options.params, options.error);
}
}
module.exports.UnauthorizedError = UnauthorizedError;
class UndefinedValueError extends AplaAPIError{
constructor(options){
super(options.msg, options.params, options.error);
}
}
module.exports.UndefinedValueError = UndefinedValueError;
class UnknownUIDError extends AplaAPIError{
constructor(options){
super(options.msg, options.params, options.error);
}
}
module.exports.UnknownUIDError = UnknownUIDError;
class VDEError extends AplaAPIError{
constructor(options){
super(options.msg, options.params, options.error);
}
}
module.exports.VDEError = VDEError;
class VDECreatedError extends AplaAPIError{
constructor(options){
super(options.msg, options.params, options.error);
}
}
module.exports.VDECreatedError = VDECreatedError;
class InvalidWalletError extends AplaAPIError{
constructor(options){
super(options.msg, options.params, options.error);
}
}
module.exports.InvalidWalletError = InvalidWalletError;
/****************** End ******************/
/****************** AplaError (general errors) description ******************/
class AplaError extends Error {
constructor (message) {
// Calling parent constructor of base Error class.
super(message);
this.name = this.constructor.name;
Error.captureStackTrace(this, this.constructor);
}
}
module.exports.AplaError = AplaError;
class RequiredParamNotPassedError extends AplaError{
constructor(paramsNotPassed){
super(`Not all required params passed to the contract: ${paramsNotPassed.join(", ")}`);
}
}
module.exports.RequiredParamNotPassedError = RequiredParamNotPassedError;
class RedundantParamPassedError extends AplaError{
constructor(paramName, contractName){
super(`Redundant param '${paramName}' passed to the contract '${contractName}'`);
}
}
module.exports.RedundantParamPassedError = RedundantParamPassedError;
class UnsupportedParamTypeError extends AplaError{
constructor(type){
super(`Param of type '${type}' is not supported`);
this.type = type;
}
}
module.exports.UnsupportedParamTypeError = UnsupportedParamTypeError;
class BadConvertResultError extends AplaError{
constructor(value, type){
super(`Failed to convert '${value}' to type '${type}'`);
}
}
module.exports.BadConvertResultError = BadConvertResultError;
/****************** End ******************/
/****************** Apla Transaction Execution Errors ******************/
module.exports.getTransactionError = (json) => {
if(!json.errmsg) return null;
return new TransactionError(json.errmsg);
};
class TransactionError extends Error{
constructor({type, error}){
super(error);
this.name = this.constructor.name;
this.type = type;
this.error = error;
Error.captureStackTrace(this, this.constructor);
}
}
/****************** End ******************/