Skip to content

Commit

Permalink
fix: better handle invalid json response (#1441)
Browse files Browse the repository at this point in the history
* better handle invalid json response

* update

* downgrade eslint to fix

* update

* only 3 attempts

* suggested changes
  • Loading branch information
justraman authored Dec 2, 2024
1 parent c02b7ef commit 1f44eda
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 34 deletions.
57 changes: 36 additions & 21 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
"@types/node": "^22.8.6",
"@typescript-eslint/eslint-plugin": "^8.12.2",
"@typescript-eslint/parser": "^8.16.0",
"eslint": "^9.15.0",
"eslint": "^9.14.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-prettier": "^5.2.1",
"prettier": "^3.2.5",
Expand Down
17 changes: 10 additions & 7 deletions src/job-handlers/process-metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,30 +98,33 @@ export default async (job: Queue.Job<JobData>, done: Queue.DoneCallback) => {
let metadata = new Metadata()

if (uriAttribute) {
const response = await connection.query<MetadataType[]>('select * from metadata.metadata where id = $1 LIMIT 1', [
const response = await em.connection.query<MetadataType[]>('select * from metadata.metadata where id = $1 LIMIT 1', [
jobData.resourceId,
])

if (response.length > 0 && response[0].uri === uriAttribute.value && !jobData.force) {
if (
response.length > 0 &&
response[0].uri === uriAttribute.value &&
typeof response[0].metadata === 'object' &&
!jobData.force
) {
externalMetadata = response[0].metadata
} else {
const externalResponse = await fetchMetadata(uriAttribute.value, job)
if (externalResponse && typeof externalResponse === 'object' && !Array.isArray(externalResponse)) {
if (externalResponse) {
if (response.length > 0) {
await connection.query(
await em.connection.query(
'update metadata.metadata set metadata = $1, uri = $2, last_updated_at = NOW() where id = $3',
[externalResponse, uriAttribute.value, jobData.resourceId]
)
} else {
await connection.query(
await em.connection.query(
'insert into metadata.metadata (id, metadata, uri, last_updated_at) values ($1, $2, $3, NOW())',
[jobData.resourceId, externalResponse, uriAttribute.value]
)
}

externalMetadata = externalResponse
} else if (response.length > 0) {
externalMetadata = response[0].metadata
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/jobs/process-metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export type JobData = { resourceId: string; type: 'token' | 'collection'; force:
export const metadataQueue = new Queue<JobData>('metadataQueue', {
defaultJobOptions: {
delay: 1000,
attempts: 4,
attempts: 3,
backoff: {
type: 'exponential',
delay: 5000,
Expand Down
14 changes: 10 additions & 4 deletions src/mappings/util/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,17 @@ export async function fetchMetadata(url: string, job: Queue.Job) {

try {
const { status, data } = await api.get(finalUrl)
if (status < 400) {
return data
if (status >= 200 && status < 300) {
if (data && typeof data === 'object' && !Array.isArray(data)) {
return data
}

job.log(`Invalid response from ${url}`)
job.log(data)
} else {
job.log(`Status code ${status} received from ${url}`)
}

throw new Error(`Failed to fetch metadata from ${url}`)
} catch (error: unknown) {
if (!Axios.isAxiosError(error)) {
Expand All @@ -54,8 +62,6 @@ export async function fetchMetadata(url: string, job: Queue.Job) {
job.log(`url: ${error.response.request.res.responseUrl} status: ${error.response.status.toString()}`)
job.log(`redirectsCount: ${error.response.request.res.redirects.length.toString()}`)
job.log(error.response.data)
} else {
job.log(`UnknownError: ${url} ${error.message}`)
}

throw error
Expand Down

0 comments on commit 1f44eda

Please sign in to comment.