fix(cid2json): Added error handler for corner case

This commit is contained in:
Chris Troutner
2025-02-09 06:39:07 -07:00
parent 8a70d6a169
commit 33a447cd61
3 changed files with 15 additions and 7 deletions
+3 -3
View File
@@ -1,6 +1,6 @@
{ {
"password": "spksihHDsyk3JUUkH3xt", "password": "eYrzProqwsAyO3CT0Ftf",
"email": "system@system.com", "email": "system@system.com",
"id": "67a8a8e25a3e45751f654a13", "id": "67a8af30c958c587f890cd8f",
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjY3YThhOGUyNWEzZTQ1NzUxZjY1NGExMyIsImlhdCI6MTczOTEwNjUzMH0.ViWmWvfJF2UnM46pRXvl0sXThfP2dNlzrmzSxpWChew" "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjY3YThhZjMwYzk1OGM1ODdmODkwY2Q4ZiIsImlhdCI6MTczOTEwODE0NH0.OFdVwH8ikbChHV0wjRpYvnL7vu46HDYS-od3FRtm9Nc"
} }
+4 -3
View File
@@ -282,8 +282,9 @@ class IpfsRESTControllerLib {
// DRY error handler // DRY error handler
handleError (ctx, err) { handleError (ctx, err) {
console.log('handleError() err.status: ', err.status) // console.log('handleError() err.status: ', err.status)
console.log('handleError() err.message: ', err.message) // console.log('handleError() err.message: ', err.message)
// If an HTTP status is specified by the buisiness logic, use that. // If an HTTP status is specified by the buisiness logic, use that.
if (err.status) { if (err.status) {
if (err.message) { if (err.message) {
@@ -292,7 +293,7 @@ class IpfsRESTControllerLib {
ctx.throw(err.status) ctx.throw(err.status)
} }
} else { } else {
console.log(`handleError() err.message: ${err.message}`) // console.log(`handleError() err.message: ${err.message}`)
// By default use a 422 error if the HTTP status is not specified. // By default use a 422 error if the HTTP status is not specified.
ctx.throw(422, err.message) ctx.throw(422, err.message)
} }
+8 -1
View File
@@ -201,8 +201,15 @@ class IpfsUseCases {
fileChunks.push(chunk) fileChunks.push(chunk)
} }
} catch (err) { } catch (err) {
// Extra logic here to look at the filename of the first file and make sure
// it ends in .json.
const name = contentArray[0].name
if (!name.endsWith('.json')) {
throw new Error(`CID ${cid} is a directory and its first file does not resolve to a valid JSON file.`)
}
// Handle CIDs that are directorys containing a JSON file. (TokenTiger.com tokens) // Handle CIDs that are directorys containing a JSON file. (TokenTiger.com tokens)
for await (const chunk of helia.fs.cat(`${cid}/${contentArray[0].name}`)) { for await (const chunk of helia.fs.cat(`${cid}/${name}`)) {
fileChunks.push(chunk) fileChunks.push(chunk)
} }
} }