Skip to content
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

Merged
merged 2 commits into from
Sep 24, 2024
Merged

Improve error handling in api #383

merged 2 commits into from
Sep 24, 2024

Conversation

Redm4x
Copy link
Contributor

@Redm4x Redm4x commented Sep 24, 2024

  • If a query fail in cacheResponse and the keepCache 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.
  • Increased the caching time for provider-attributes.json from 30s to 5min. Also enabled the keepCache param for both queries in githubService.ts so that the old cached version is returned if the query fails.
  • Use the new logger in the caching helper
  • Added parameter validation in the address deployments endpoint

@Redm4x Redm4x force-pushed the bugfixes/improve-error-handling branch from 2c6f2e1 to ecf77fd Compare September 24, 2024 14:37
Copy link
Contributor

@ygrishajev ygrishajev left a 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

import MemoryCacheEngine from "./memoryCacheEngine";

const logger = new LoggerService({ context: "CachingHelpers" });
Copy link
Contributor

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 :)

Suggested change
const logger = new LoggerService({ context: "CachingHelpers" });
const logger = new LoggerService({ context: "Caching" });

@@ -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}`);
Copy link
Contributor

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

Copy link
Contributor Author

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")) {
Copy link
Contributor

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)

Copy link
Contributor Author

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,
Copy link
Contributor

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

Copy link
Contributor Author

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

@Redm4x Redm4x force-pushed the bugfixes/improve-error-handling branch from ecf77fd to cdf6dee Compare September 24, 2024 15:56
@Redm4x Redm4x merged commit 8dfc23e into main Sep 24, 2024
5 checks passed
@Redm4x Redm4x deleted the bugfixes/improve-error-handling branch September 24, 2024 18:59
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.

3 participants