Move RelayError into its own file, add helper methods

This commit is contained in:
Alex Gleason 2024-05-14 14:38:05 -05:00
parent 4029971407
commit ecfea827e1
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
4 changed files with 31 additions and 21 deletions

24
src/RelayError.ts Normal file
View File

@ -0,0 +1,24 @@
import { NostrRelayOK } from '@nostrify/nostrify';
export type RelayErrorPrefix = 'duplicate' | 'pow' | 'blocked' | 'rate-limited' | 'invalid' | 'error';
/** NIP-01 command line result. */
export class RelayError extends Error {
constructor(prefix: RelayErrorPrefix, message: string) {
super(`${prefix}: ${message}`);
}
/** Construct a RelayError from the reason message. */
static fromReason(reason: string): RelayError {
const [prefix, ...rest] = reason.split(': ');
return new RelayError(prefix as RelayErrorPrefix, rest.join(': '));
}
/** Throw a new RelayError if the OK message is false. */
static assert(msg: NostrRelayOK): void {
const [_, _eventId, ok, reason] = msg;
if (!ok) {
throw RelayError.fromReason(reason);
}
}
}

View File

@ -10,6 +10,7 @@ import {
} from '@nostrify/nostrify';
import { relayInfoController } from '@/controllers/nostr/relay-info.ts';
import * as pipeline from '@/pipeline.ts';
import { RelayError } from '@/RelayError.ts';
import { Storages } from '@/storages.ts';
import type { AppController } from '@/app.ts';
@ -95,7 +96,7 @@ function connectStream(socket: WebSocket) {
await pipeline.handleEvent(event, AbortSignal.timeout(1000));
send(['OK', event.id, true, '']);
} catch (e) {
if (e instanceof pipeline.RelayError) {
if (e instanceof RelayError) {
send(['OK', event.id, false, e.message]);
} else {
send(['OK', event.id, false, 'error: something went wrong']);

View File

@ -10,6 +10,7 @@ import { deleteAttachedMedia } from '@/db/unattached-media.ts';
import { DittoEvent } from '@/interfaces/DittoEvent.ts';
import { isEphemeralKind } from '@/kinds.ts';
import { DVM } from '@/pipeline/DVM.ts';
import { RelayError } from '@/RelayError.ts';
import { updateStats } from '@/stats.ts';
import { hydrateEvents, purifyEvent } from '@/storages/hydrate.ts';
import { Storages } from '@/storages.ts';
@ -71,15 +72,7 @@ async function policyFilter(event: NostrEvent): Promise<void> {
const result = await policy.call(event);
debug(JSON.stringify(result));
const [_, _eventId, ok, reason] = result;
if (!ok) {
const [prefix, ...rest] = reason.split(': ');
if (['duplicate', 'pow', 'blocked', 'rate-limited', 'invalid'].includes(prefix)) {
throw new RelayError(prefix as RelayErrorPrefix, rest.join(': '));
} else {
throw new RelayError('error', rest.join(': '));
}
}
RelayError.assert(result);
}
/** Encounter the event, and return whether it has already been encountered. */
@ -286,13 +279,4 @@ async function streamOut(event: NostrEvent): Promise<void> {
}
}
type RelayErrorPrefix = 'duplicate' | 'pow' | 'blocked' | 'rate-limited' | 'invalid' | 'error';
/** NIP-20 command line result. */
class RelayError extends Error {
constructor(prefix: RelayErrorPrefix, message: string) {
super(`${prefix}: ${message}`);
}
}
export { handleEvent, RelayError };
export { handleEvent };

View File

@ -9,6 +9,7 @@ import { z } from 'zod';
import { type AppContext } from '@/app.ts';
import { Conf } from '@/config.ts';
import * as pipeline from '@/pipeline.ts';
import { RelayError } from '@/RelayError.ts';
import { AdminSigner } from '@/signers/AdminSigner.ts';
import { APISigner } from '@/signers/APISigner.ts';
import { Storages } from '@/storages.ts';
@ -106,7 +107,7 @@ async function publishEvent(event: NostrEvent, c: AppContext): Promise<NostrEven
await pipeline.handleEvent(event, c.req.raw.signal);
await Storages.client.event(event);
} catch (e) {
if (e instanceof pipeline.RelayError) {
if (e instanceof RelayError) {
throw new HTTPException(422, {
res: c.json({ error: e.message }, 422),
});