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'; } from '@nostrify/nostrify';
import { relayInfoController } from '@/controllers/nostr/relay-info.ts'; import { relayInfoController } from '@/controllers/nostr/relay-info.ts';
import * as pipeline from '@/pipeline.ts'; import * as pipeline from '@/pipeline.ts';
import { RelayError } from '@/RelayError.ts';
import { Storages } from '@/storages.ts'; import { Storages } from '@/storages.ts';
import type { AppController } from '@/app.ts'; import type { AppController } from '@/app.ts';
@ -95,7 +96,7 @@ function connectStream(socket: WebSocket) {
await pipeline.handleEvent(event, AbortSignal.timeout(1000)); await pipeline.handleEvent(event, AbortSignal.timeout(1000));
send(['OK', event.id, true, '']); send(['OK', event.id, true, '']);
} catch (e) { } catch (e) {
if (e instanceof pipeline.RelayError) { if (e instanceof RelayError) {
send(['OK', event.id, false, e.message]); send(['OK', event.id, false, e.message]);
} else { } else {
send(['OK', event.id, false, 'error: something went wrong']); 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 { DittoEvent } from '@/interfaces/DittoEvent.ts';
import { isEphemeralKind } from '@/kinds.ts'; import { isEphemeralKind } from '@/kinds.ts';
import { DVM } from '@/pipeline/DVM.ts'; import { DVM } from '@/pipeline/DVM.ts';
import { RelayError } from '@/RelayError.ts';
import { updateStats } from '@/stats.ts'; import { updateStats } from '@/stats.ts';
import { hydrateEvents, purifyEvent } from '@/storages/hydrate.ts'; import { hydrateEvents, purifyEvent } from '@/storages/hydrate.ts';
import { Storages } from '@/storages.ts'; import { Storages } from '@/storages.ts';
@ -71,15 +72,7 @@ async function policyFilter(event: NostrEvent): Promise<void> {
const result = await policy.call(event); const result = await policy.call(event);
debug(JSON.stringify(result)); debug(JSON.stringify(result));
const [_, _eventId, ok, reason] = result; RelayError.assert(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(': '));
}
}
} }
/** Encounter the event, and return whether it has already been encountered. */ /** 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'; export { handleEvent };
/** NIP-20 command line result. */
class RelayError extends Error {
constructor(prefix: RelayErrorPrefix, message: string) {
super(`${prefix}: ${message}`);
}
}
export { handleEvent, RelayError };

View File

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