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

Prosopo api error error code fixes #1234

Merged
merged 4 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}`, status: 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} `, status: 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 },
HughParry marked this conversation as resolved.
Show resolved Hide resolved
})
}

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 @@
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 All @@ -33,7 +33,7 @@
return handleResponse(response)
}

export async function updateDataset(BASE_URL: string, additionalHeaders: Record<string, string> = {}, jsonFile: any) {

Check warning on line 36 in provider-gui/services/api/api.ts

View workflow job for this annotation

GitHub Actions / check

Unexpected any. Specify a different type
const jsonFileString = JSON.stringify(jsonFile)
const response = await fetch(`${BASE_URL}${AdminApiPaths.UpdateDataset}`, {
method: 'POST',
Expand Down
Loading