fedimbed: better error handling
This commit is contained in:
		
							parent
							
								
									909bec67e1
								
							
						
					
					
						commit
						e0c699acf5
					
				
					 1 changed files with 10 additions and 4 deletions
				
			
		| 
						 | 
					@ -71,20 +71,26 @@ async function processUrl(msg, url) {
 | 
				
			||||||
  let content, cw, author;
 | 
					  let content, cw, author;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  // Fetch post
 | 
					  // Fetch post
 | 
				
			||||||
  const postData = await fetch(url, {
 | 
					  const rawPostData = await fetch(url, {
 | 
				
			||||||
    headers: {
 | 
					    headers: {
 | 
				
			||||||
      "User-Agent": FRIENDLY_USERAGENT,
 | 
					      "User-Agent": FRIENDLY_USERAGENT,
 | 
				
			||||||
      Accept: "application/activity+json",
 | 
					      Accept: "application/activity+json",
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
  })
 | 
					  })
 | 
				
			||||||
    .then((res) => res.json())
 | 
					    .then((res) => res.text())
 | 
				
			||||||
    .catch((err) => {
 | 
					    .catch((err) => {
 | 
				
			||||||
      logger.error("fedimbed", `Failed to fetch "${url}" as AS2: ${err}`);
 | 
					      logger.error("fedimbed", `Failed to fetch "${url}" as AS2: ${err}`);
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  if (postData.error) {
 | 
					  let postData;
 | 
				
			||||||
 | 
					  if (rawPostData.startsWith("{") {
 | 
				
			||||||
 | 
					    postData = JSON.parse(rawPostData);
 | 
				
			||||||
 | 
					  } else {
 | 
				
			||||||
 | 
					    logger.error("fedimbed", `Got non-JSON for "${url}": ${rawPostData}`);
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  if (postData?.error) {
 | 
				
			||||||
    logger.error("fedimbed", `Received error for "${url}": ${postData.error}`);
 | 
					    logger.error("fedimbed", `Received error for "${url}": ${postData.error}`);
 | 
				
			||||||
    return;
 | 
					 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  if (!postData) {
 | 
					  if (!postData) {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue