diff --git a/dev/flux/src/errorHandler.ts b/dev/flux/src/errorHandler.ts index 2f554eae90..6ba399dff6 100644 --- a/dev/flux/src/errorHandler.ts +++ b/dev/flux/src/errorHandler.ts @@ -18,14 +18,16 @@ export async function streamToJson(stream: ReadableStream): Promise< export const errorHandler = async (response: Response) => { if (!response.ok) { - throw new ProsopoApiError('API.BAD_REQUEST', { context: { error: `HTTP error! status: ${response.status}` } }) + throw new ProsopoApiError('API.BAD_REQUEST', { + context: { error: `HTTP error! status: ${response.status}`, code: response.status }, + }) } if (response.body && !response.bodyUsed) { const data = await streamToJson(response.body) if (data.status === 'error') { throw new ProsopoApiError('API.BAD_REQUEST', { - context: { error: `HTTP error! status: ${data.data.message} ` }, + context: { error: `HTTP error! status: ${data.data.message} `, code: response.status }, }) } return data as T diff --git a/packages/datasets/src/captcha/util.ts b/packages/datasets/src/captcha/util.ts index 17235567a1..d1a27f4182 100644 --- a/packages/datasets/src/captcha/util.ts +++ b/packages/datasets/src/captcha/util.ts @@ -11,13 +11,13 @@ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. -import { ProsopoApiError, ProsopoEnvError } from '@prosopo/common' +import { ProsopoDatasetError, ProsopoEnvError } from '@prosopo/common' export async function downloadImage(url: string): Promise { try { const response = await fetch(url) if (!response.ok) { - throw new ProsopoApiError('API.BAD_REQUEST', { + throw new ProsopoDatasetError('API.BAD_REQUEST', { context: { error: `Network response was not ok, status: ${response.status}`, url }, }) } diff --git a/packages/procaptcha/src/modules/Manager.ts b/packages/procaptcha/src/modules/Manager.ts index d4d1fea4c9..e682fc53ea 100644 --- a/packages/procaptcha/src/modules/Manager.ts +++ b/packages/procaptcha/src/modules/Manager.ts @@ -30,15 +30,14 @@ import { import { ApiPromise } from '@polkadot/api/promise/Api' import { ExtensionWeb2, ExtensionWeb3 } from '@prosopo/account' import { Keyring } from '@polkadot/keyring' +import { ProsopoCaptchaContract, wrapQuery } from '@prosopo/contract' import { - ProsopoApiError, ProsopoContractError, ProsopoDatasetError, ProsopoEnvError, ProsopoError, trimProviderUrl, } from '@prosopo/common' -import { ProsopoCaptchaContract, wrapQuery } from '@prosopo/contract' import { ProviderApi } from '@prosopo/api' import { RandomProvider } from '@prosopo/captcha-contract/types-returns' import { WsProvider } from '@polkadot/rpc-provider/ws' @@ -231,7 +230,7 @@ export function Manager( const challenge = await captchaApi.getCaptchaChallenge() if (challenge.captchas.length <= 0) { - throw new ProsopoApiError('DEVELOPER.PROVIDER_NO_CAPTCHA') + throw new ProsopoDatasetError('DEVELOPER.PROVIDER_NO_CAPTCHA') } // setup timeout, taking the timeout from the individual captcha or the global default @@ -452,7 +451,9 @@ export function Manager( const getCaptchaApi = () => { if (!state.captchaApi) { - throw new ProsopoApiError('API.UNKNOWN', { context: { error: 'Captcha api not set', state } }) + throw new ProsopoEnvError('API.UNKNOWN', { + context: { error: 'Captcha api not set', state }, + }) } return state.captchaApi } diff --git a/packages/provider/src/api/authMiddleware.ts b/packages/provider/src/api/authMiddleware.ts index df55146c2d..34ef4ce56a 100644 --- a/packages/provider/src/api/authMiddleware.ts +++ b/packages/provider/src/api/authMiddleware.ts @@ -46,12 +46,14 @@ const extractHeaders = (req: Request) => { if (!signature || !blocknumber) { throw new ProsopoApiError('CONTRACT.INVALID_DATA_FORMAT', { - context: { error: 'Missing signature or block number' }, + context: { error: 'Missing signature or block number', code: 400 }, }) } if (Array.isArray(signature) || Array.isArray(blocknumber) || !isHex(signature)) { - throw new ProsopoApiError('CONTRACT.INVALID_DATA_FORMAT', { context: { error: 'Invalid header format' } }) + throw new ProsopoApiError('CONTRACT.INVALID_DATA_FORMAT', { + context: { error: 'Invalid header format', code: 400 }, + }) } return { signature, blocknumber } @@ -75,6 +77,7 @@ const verifyBlockNumber = async (blockNumber: string, tasks: Tasks) => { throw new ProsopoApiError('API.BAD_REQUEST', { context: { error: `Invalid block number ${parsedBlockNumber}, current block number is ${currentBlockNumber}`, + code: 400, }, }) } @@ -84,6 +87,8 @@ const verifySignature = (signature: string, blockNumber: string, pair: KeyringPa const u8Sig = hexToU8a(signature) if (!pair.verify(blockNumber, u8Sig, pair.publicKey)) { - throw new ProsopoApiError('GENERAL.INVALID_SIGNATURE', { context: { error: 'Signature verification failed' } }) + throw new ProsopoApiError('GENERAL.INVALID_SIGNATURE', { + context: { error: 'Signature verification failed', code: 401 }, + }) } } diff --git a/provider-gui/services/api/api.ts b/provider-gui/services/api/api.ts index 6b38495e49..b0032defdd 100644 --- a/provider-gui/services/api/api.ts +++ b/provider-gui/services/api/api.ts @@ -17,7 +17,7 @@ import { ProsopoApiError } from '@prosopo/common' async function handleResponse(response: Response) { if (!response.ok) { const errorMessage = await response.text() - throw new ProsopoApiError('API.BAD_REQUEST', { context: { error: errorMessage } }) + throw new ProsopoApiError('API.BAD_REQUEST', { context: { error: errorMessage, code: 400 } }) } return response.json() }