Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 6 additions & 36 deletions packages/backend-core/src/errors/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,45 +97,15 @@ export class NotImplementedError extends HTTPError {
}

// LICENSING

export class UsageLimitError extends HTTPError {
limitName: string

constructor(message: string, limitName: string) {
super(message, 400, ErrorCode.USAGE_LIMIT_EXCEEDED)
this.limitName = limitName
}

getPublicError() {
return {
limitName: this.limitName,
}
export class FeatureDisabledError extends Error {
constructor(feature: string) {
super(`Feature disabled: '${feature}'`)
}
}

export class FeatureDisabledError extends HTTPError {
featureName: string

constructor(message: string, featureName: string) {
super(message, 400, ErrorCode.FEATURE_DISABLED)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As mentioned in the other PR - just update the code.

this.featureName = featureName
}

getPublicError() {
return {
featureName: this.featureName,
}
}
}

// AUTH

export class InvalidAPIKeyError extends BudibaseError {
constructor() {
super(
"Invalid API key - may need re-generated, or user doesn't exist",
ErrorCode.INVALID_API_KEY
)
export class UsageLimitError extends Error {
constructor(limitName: string) {
super(`Usage limit exceeded: '${limitName}'`)
}
}

Expand Down
8 changes: 4 additions & 4 deletions packages/backend-core/src/middleware/authenticated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ import env from "../environment"
import {
Ctx,
EndpointMatcher,
ErrorCode,
APIWarningCode,
LoginMethod,
SessionCookie,
User,
} from "@budibase/types"
import { InvalidAPIKeyError } from "../errors"
import { InvalidAPIKeyWarning } from "../warnings"
import tracer from "dd-trace"
import type { Middleware, Next } from "koa"

Expand Down Expand Up @@ -91,7 +91,7 @@ async function checkApiKey(
}),
}
} else {
throw new InvalidAPIKeyError()
throw new InvalidAPIKeyWarning()
}
})
}
Expand Down Expand Up @@ -242,7 +242,7 @@ export function authenticated(
// invalid token, clear the cookie
if (err?.name === "JsonWebTokenError") {
clearCookie(ctx, Cookie.Auth)
} else if (err?.code === ErrorCode.INVALID_API_KEY) {
} else if (err?.code === APIWarningCode.INVALID_API_KEY) {
ctx.throw(403, err.message)
}
// allow configuring for public access
Expand Down
1 change: 1 addition & 0 deletions packages/backend-core/src/warnings/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./warnings"
54 changes: 54 additions & 0 deletions packages/backend-core/src/warnings/warnings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// BASE

import { APIWarningCode } from "@budibase/types"

export abstract class APIWarning extends Error {
code: string

constructor(message: string, code: string) {
super(message)
this.code = code
}

protected getPublicWarning?(): any
}

// AUTH

export class InvalidAPIKeyWarning extends APIWarning {
constructor() {
super("Invalid API key", APIWarningCode.INVALID_API_KEY)
}
}

// LICENSING

export class UsageLimitWarning extends APIWarning {
limitName: string

constructor(message: string, limitName: string) {
super(message, APIWarningCode.USAGE_LIMIT_EXCEEDED)
this.limitName = limitName
}

getPublicWarning() {
return {
limitName: this.limitName,
}
}
}

export class FeatureDisabledWarning extends APIWarning {
featureName: string

constructor(message: string, featureName: string) {
super(message, APIWarningCode.FEATURE_DISABLED)
this.featureName = featureName
}

getPublicWarning() {
return {
featureName: this.featureName,
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import { createEventDispatcher } from "svelte"
import { API } from "@/api"
import { ErrorCode, type EnrichedBinding } from "@budibase/types"
import { APIWarningCode, type EnrichedBinding } from "@budibase/types"
import analytics, { Events } from "@/analytics"
import AiInput from "../ai/AIInput.svelte"

Expand Down Expand Up @@ -51,7 +51,7 @@
} catch (e: any) {
console.error(e)

if ("code" in e && e.code === ErrorCode.USAGE_LIMIT_EXCEEDED) {
if ("code" in e && e.code === APIWarningCode.USAGE_LIMIT_EXCEEDED) {
notifications.error(
"Monthly usage limit reached. We're exploring options to expand this soon. Questions? Contact [email protected]"
)
Expand Down
4 changes: 2 additions & 2 deletions packages/server/src/api/controllers/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
DeleteWorkspaceResponse,
DuplicateWorkspaceRequest,
DuplicateWorkspaceResponse,
ErrorCode,
APIWarningCode,
FetchAppDefinitionResponse,
FetchAppPackageResponse,
FetchPublishedAppsResponse,
Expand Down Expand Up @@ -638,7 +638,7 @@ async function workspacePostCreate(
return quotas.addRows(rowCount)
})
} catch (err: any) {
if (err.code && err.code === ErrorCode.USAGE_LIMIT_EXCEEDED) {
if (err.code && err.code === APIWarningCode.USAGE_LIMIT_EXCEEDED) {
// this import resulted in row usage exceeding the quota
// delete the app
// skip pre and post-steps as no rows have been added to quotas yet
Expand Down
3 changes: 0 additions & 3 deletions packages/types/src/api/web/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,5 @@ export interface APIError {
}

export enum ErrorCode {
USAGE_LIMIT_EXCEEDED = "usage_limit_exceeded",
FEATURE_DISABLED = "feature_disabled",
INVALID_API_KEY = "invalid_api_key",
HTTP = "http",
}
1 change: 1 addition & 0 deletions packages/types/src/api/web/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export * from "./cookies"
export * from "./debug"
export * from "./dev"
export * from "./errors"
export * from "./warnings"
export * from "./global"
export * from "./pagination"
export * from "./plugins"
Expand Down
10 changes: 10 additions & 0 deletions packages/types/src/api/web/warnings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export interface APIWarning {
message: string
status: number
}

export enum APIWarningCode {
USAGE_LIMIT_EXCEEDED = "usage_limit_exceeded",
FEATURE_DISABLED = "feature_disabled",
INVALID_API_KEY = "invalid_api_key",
}
4 changes: 2 additions & 2 deletions packages/worker/src/api/controllers/global/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import {
DeleteInviteUsersResponse,
DeleteUserResponse,
EditUserPermissionsResponse,
ErrorCode,
APIWarningCode,
FetchUsersResponse,
FindUserResponse,
GetUserInvitesResponse,
Expand Down Expand Up @@ -614,7 +614,7 @@ export const inviteAccept = async (
}
)
} catch (err: any) {
if (err.code === ErrorCode.USAGE_LIMIT_EXCEEDED) {
if (err.code === APIWarningCode.USAGE_LIMIT_EXCEEDED) {
// explicitly re-throw limit exceeded errors
ctx.throw(400, err)
}
Expand Down
Loading