Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 4, 2025

When unauthenticated users attempt deletion requests, the API returns HTML error pages instead of JSON. The frontend cannot parse HTML as JSON, triggering catch blocks that show "网络错误或服务器异常,请稍后再试" instead of the actual error message "需要登录".

Changes

Modified middleware/error_display.js to detect API requests and return JSON responses:

export default (err, req, res, next) => {
	const isJsonRequest = req.path.startsWith('/api/') ||
	                      req.xhr || 
	                      req.headers.accept?.includes('application/json');
	
	if (isJsonRequest) {
		return res.json(makeResponse(false, { message: err.message }));
	}
	
	res.render('system/error.njk', { title: "错误", error_message: err.message });
}

Impact

Fixes authentication error handling for all API endpoints using requireLogin or requireAdmin middleware:

  • Deletion requests (/api/deletion/:type/:id)
  • Notification endpoints (/notification/api, /notification/:id/read, /notification/read-all)
  • Admin API endpoints (/admin/api/*)

Users now see "需要登录" instead of generic network errors when attempting authenticated actions while logged out.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • spider-benben.imken.dev
    • Triggering command: node -e import('./app.js').then(() => console.log('OK')).catch(e => console.error(e)) (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

未登录时申请删除会提示「网络错误或服务器异常,请稍后再试
」而不是「需要登录」,请修复这个问题。


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI self-assigned this Nov 4, 2025
Copilot AI changed the title [WIP] Fix error message for delete request when not logged in Fix API authentication errors returning HTML instead of JSON Nov 4, 2025
Copilot AI requested a review from CuteMurasame November 4, 2025 11:25
Copilot finished work on behalf of CuteMurasame November 4, 2025 11:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants