fix: show 404 page if /api/v2/meta/page/{page} can't find a file
This commit is contained in:
		
							parent
							
								
									0c6e3bf38f
								
							
						
					
					
						commit
						c8e4078b35
					
				
					 6 changed files with 17 additions and 9 deletions
				
			
		|  | @ -58,9 +58,16 @@ public partial class MetaController(Config config) : ApiControllerBase | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         string path = Path.Join(Directory.GetCurrentDirectory(), "static-pages", $"{page}.md"); |         string path = Path.Join(Directory.GetCurrentDirectory(), "static-pages", $"{page}.md"); | ||||||
|  |         try | ||||||
|  |         { | ||||||
|             string text = await System.IO.File.ReadAllTextAsync(path, ct); |             string text = await System.IO.File.ReadAllTextAsync(path, ct); | ||||||
|             return Ok(text); |             return Ok(text); | ||||||
|         } |         } | ||||||
|  |         catch (FileNotFoundException) | ||||||
|  |         { | ||||||
|  |             throw new ApiError.NotFound("Page not found", code: ErrorCode.PageNotFound); | ||||||
|  |         } | ||||||
|  |     } | ||||||
| 
 | 
 | ||||||
|     [HttpGet("/api/v2/coffee")] |     [HttpGet("/api/v2/coffee")] | ||||||
|     public IActionResult BrewCoffee() => |     public IActionResult BrewCoffee() => | ||||||
|  |  | ||||||
|  | @ -164,6 +164,7 @@ public enum ErrorCode | ||||||
|     GenericApiError, |     GenericApiError, | ||||||
|     UserNotFound, |     UserNotFound, | ||||||
|     MemberNotFound, |     MemberNotFound, | ||||||
|  |     PageNotFound, | ||||||
|     AccountAlreadyLinked, |     AccountAlreadyLinked, | ||||||
|     LastAuthMethod, |     LastAuthMethod, | ||||||
|     InvalidReportTarget, |     InvalidReportTarget, | ||||||
|  |  | ||||||
|  | @ -21,12 +21,8 @@ Sentry.init({ | ||||||
| }); | }); | ||||||
| 
 | 
 | ||||||
| export const handleError: HandleServerError = async ({ error, status, message }) => { | export const handleError: HandleServerError = async ({ error, status, message }) => { | ||||||
| 	// as far as i know, sentry IDs are just UUIDs with the dashes removed. use those here as well
 |  | ||||||
| 	let id = crypto.randomUUID().replaceAll("-", ""); |  | ||||||
| 
 |  | ||||||
| 	if (error instanceof ApiError) { | 	if (error instanceof ApiError) { | ||||||
| 		return { | 		return { | ||||||
| 			error_id: id, |  | ||||||
| 			status: error.raw?.status || status, | 			status: error.raw?.status || status, | ||||||
| 			message: error.raw?.message || "Unknown error", | 			message: error.raw?.message || "Unknown error", | ||||||
| 			code: error.code, | 			code: error.code, | ||||||
|  | @ -34,11 +30,11 @@ export const handleError: HandleServerError = async ({ error, status, message }) | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	if (status >= 400 && status <= 499) { | 	if (status >= 400 && status <= 499) { | ||||||
| 		return { error_id: id, status, message, code: ErrorCode.GenericApiError }; | 		return { status, message, code: ErrorCode.GenericApiError }; | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	// client errors and backend API errors just clog up sentry, so we don't send those.
 | 	// client errors and backend API errors just clog up sentry, so we don't send those.
 | ||||||
| 	id = Sentry.captureException(error, { | 	const id = Sentry.captureException(error, { | ||||||
| 		mechanism: { | 		mechanism: { | ||||||
| 			type: "sveltekit", | 			type: "sveltekit", | ||||||
| 			handled: false, | 			handled: false, | ||||||
|  |  | ||||||
|  | @ -43,6 +43,7 @@ export enum ErrorCode { | ||||||
| 	MemberNotFound = "MEMBER_NOT_FOUND", | 	MemberNotFound = "MEMBER_NOT_FOUND", | ||||||
| 	AccountAlreadyLinked = "ACCOUNT_ALREADY_LINKED", | 	AccountAlreadyLinked = "ACCOUNT_ALREADY_LINKED", | ||||||
| 	LastAuthMethod = "LAST_AUTH_METHOD", | 	LastAuthMethod = "LAST_AUTH_METHOD", | ||||||
|  | 	PageNotFound = "PAGE_NOT_FOUND", | ||||||
| 	// This code isn't actually returned by the API
 | 	// This code isn't actually returned by the API
 | ||||||
| 	Non204Response = "(non 204 response)", | 	Non204Response = "(non 204 response)", | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -29,6 +29,8 @@ export default function errorDescription(t: TranslateFn, code: ErrorCode): strin | ||||||
| 			return t("error.account-already-linked"); | 			return t("error.account-already-linked"); | ||||||
| 		case ErrorCode.LastAuthMethod: | 		case ErrorCode.LastAuthMethod: | ||||||
| 			return t("error.last-auth-method"); | 			return t("error.last-auth-method"); | ||||||
|  | 		case ErrorCode.PageNotFound: | ||||||
|  | 			return t("error.page-not-found"); | ||||||
| 		case ErrorCode.Non204Response: | 		case ErrorCode.Non204Response: | ||||||
| 			return t("error.generic-error"); | 			return t("error.generic-error"); | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  | @ -120,7 +120,8 @@ | ||||||
| 		"400-description": "Something went wrong with your request. This error should never land you on this page, so it's probably a bug.", | 		"400-description": "Something went wrong with your request. This error should never land you on this page, so it's probably a bug.", | ||||||
| 		"500-description": "Something went wrong on the server. Please try again later.", | 		"500-description": "Something went wrong on the server. Please try again later.", | ||||||
| 		"unknown-status-description": "Something went wrong, but we're not sure what. Please try again.", | 		"unknown-status-description": "Something went wrong, but we're not sure what. Please try again.", | ||||||
| 		"error-id": "If you report this error to the developers, please give them this ID:" | 		"error-id": "If you report this error to the developers, please give them this ID:", | ||||||
|  | 		"page-not-found": "No page exists at this URL." | ||||||
| 	}, | 	}, | ||||||
| 	"settings": { | 	"settings": { | ||||||
| 		"general-information-tab": "General information", | 		"general-information-tab": "General information", | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue