Skip to content

Commit

Permalink
Prosopo api error error code fixes (#1234)
Browse files Browse the repository at this point in the history
* Adding statuscodes to errors thrown from ProsopoApiError

* Changing status to code

---------

Co-authored-by: Chris <forgetso86@gmail.com>
  • Loading branch information
HughParry and forgetso authored May 21, 2024
1 parent 61aa4ea commit 3716083
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 12 deletions.
6 changes: 4 additions & 2 deletions dev/flux/src/errorHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@ export async function streamToJson(stream: ReadableStream<Uint8Array>): Promise<

export const errorHandler = async <T>(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
Expand Down
4 changes: 2 additions & 2 deletions packages/datasets/src/captcha/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Uint8Array> {
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 },
})
}
Expand Down
9 changes: 5 additions & 4 deletions packages/procaptcha/src/modules/Manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
11 changes: 8 additions & 3 deletions packages/provider/src/api/authMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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,
},
})
}
Expand All @@ -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 },
})
}
}
2 changes: 1 addition & 1 deletion provider-gui/services/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down

0 comments on commit 3716083

Please sign in to comment.