-
Notifications
You must be signed in to change notification settings - Fork 59
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve error handling in api #383
Conversation
2c6f2e1
to
ecf77fd
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
all the comments are nice to have from my perspective
apps/api/src/caching/helpers.ts
Outdated
import MemoryCacheEngine from "./memoryCacheEngine"; | ||
|
||
const logger = new LoggerService({ context: "CachingHelpers" }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not too hard on this, but eventually this would look better on logs :)
const logger = new LoggerService({ context: "CachingHelpers" }); | |
const logger = new LoggerService({ context: "Caching" }); |
apps/api/src/caching/helpers.ts
Outdated
@@ -30,20 +33,24 @@ export const Memoize = (options?: MemoizeOptions) => (target: object, propertyNa | |||
export async function cacheResponse<T>(seconds: number, key: string, refreshRequest: () => Promise<T>, keepData?: boolean): Promise<T> { | |||
const duration = seconds * 1000; | |||
const cachedObject = cacheEngine.getFromCache(key) as CachedObject<T> | undefined; | |||
// console.log(`Cache key: ${key}`); | |||
// logger.info(`Cache key: ${key}`); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why commented? perhaps not needed? You may also use logger.debug
and uncomment if you want to avoid it in some envs
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I think that was to quickly uncomment when debugging. I'll switch to using logger.debug
.
} | ||
} | ||
}); | ||
|
||
export default new OpenAPIHono().openapi(route, async c => { | ||
if (!isValidBech32Address(c.req.valid("param").address, "akash")) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I started using http errors in other places and those are handled by a global error handler. It would also look a bit better generally:
import assert from "http-assert";
assert(isValidBech32Address(c.req.valid("param").address, "Akash"), 400, 'Invalid address)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wont do it in this PR, but yeah we could change all endpoints to using http-assert
in a separate PR
@@ -22,19 +22,25 @@ export function getOctokit() { | |||
export const getProviderAttributesSchema = async (): Promise<ProviderAttributesSchema> => { | |||
// Fetching provider attributes schema | |||
const response = await cacheResponse( | |||
30, | |||
60 * 5, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
import { secondsInMinute } from "date-fns/constants";
5 * secondsInMinute
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I switched to using date-fns/minutesToSeconds
ecf77fd
to
cdf6dee
Compare
cacheResponse
and thekeepCache
param was not used, re-throw the exception instead of catching and returning undefined. This prevents a second error from happening further in the flow because of the undefined value.provider-attributes.json
from 30s to 5min. Also enabled thekeepCache
param for both queries ingithubService.ts
so that the old cached version is returned if the query fails.