improve AP error formtting

This commit is contained in:
Hazelnoot 2024-11-21 10:55:27 -05:00 committed by fly_mc
parent 3b20a73d58
commit 3be2143a9a
4 changed files with 31 additions and 30 deletions

View file

@ -428,7 +428,7 @@ export class ApInboxService {
if (isPost(object)) { if (isPost(object)) {
await this.createNote(resolver, actor, object, false, activity); await this.createNote(resolver, actor, object, false, activity);
} else { } else {
return `skip: Unsupported type for Create: ${getApType(object)} (object ${getNullableApId(object)})`; return `skip: Unsupported type for Create: ${getApType(object)} ${getNullableApId(object)}`;
} }
} }
@ -815,7 +815,7 @@ export class ApInboxService {
await this.apNoteService.updateNote(object, resolver).catch(err => console.error(err)); await this.apNoteService.updateNote(object, resolver).catch(err => console.error(err));
return 'ok: Note updated'; return 'ok: Note updated';
} else { } else {
return `skip: Unsupported type for Update: ${getApType(object)} (object ${getNullableApId(object)})`; return `skip: Unsupported type for Update: ${getApType(object)} ${getNullableApId(object)}`;
} }
} }

View file

@ -156,16 +156,16 @@ export class ApNoteService {
} }
if (!checkHttps(note.id)) { if (!checkHttps(note.id)) {
throw new UnrecoverableError(`unexpected schema of note url ${url}: ${entryUri}`); throw new UnrecoverableError(`unexpected schema of note.id ${note.id} in ${entryUri}`);
} }
if (url != null) { if (url != null) {
if (!checkHttps(url)) { if (!checkHttps(url)) {
throw new UnrecoverableError('unexpected schema of note url: ' + url); throw new UnrecoverableError(`unexpected schema of note.url ${url} in ${entryUri}`);
} }
// if (this.utilityService.punyHost(url) !== this.utilityService.punyHost(note.id)) { // if (this.utilityService.punyHost(url) !== this.utilityService.punyHost(note.id)) {
// throw new Error(`note url & uri host mismatch: note url: ${url}, note uri: ${note.id}`); // throw new Error(`note url <> uri host mismatch: ${url} <> ${note.id} in ${entryUri}`);
// } // }
} }
@ -173,7 +173,7 @@ export class ApNoteService {
// 投稿者をフェッチ // 投稿者をフェッチ
if (note.attributedTo == null) { if (note.attributedTo == null) {
throw new UnrecoverableError(`invalid note.attributedTo ${note.attributedTo}: ${entryUri}`); throw new UnrecoverableError(`invalid note.attributedTo ${note.attributedTo} in ${entryUri}`);
} }
const uri = getOneApId(note.attributedTo); const uri = getOneApId(note.attributedTo);
@ -248,13 +248,13 @@ export class ApNoteService {
.then(x => { .then(x => {
if (x == null) { if (x == null) {
this.logger.warn('Specified inReplyTo, but not found'); this.logger.warn('Specified inReplyTo, but not found');
throw new Error(`could not fetch inReplyTo ${note.inReplyTo}: ${entryUri}`); throw new Error(`could not fetch inReplyTo ${note.inReplyTo} for note ${entryUri}`);
} }
return x; return x;
}) })
.catch(async err => { .catch(async err => {
this.logger.warn(`error ${err.statusCode ?? err} fetching inReplyTo ${note.inReplyTo}: ${entryUri}`); this.logger.warn(`error ${err.statusCode ?? err} fetching inReplyTo ${note.inReplyTo} for note ${entryUri}`);
throw err; throw err;
}) })
: null; : null;
@ -345,7 +345,7 @@ export class ApNoteService {
this.logger.info('The note is already inserted while creating itself, reading again'); this.logger.info('The note is already inserted while creating itself, reading again');
const duplicate = await this.fetchNote(value); const duplicate = await this.fetchNote(value);
if (!duplicate) { if (!duplicate) {
throw new Error(`The note creation failed with duplication error even when there is no duplication, ${entryUri}`); throw new Error(`The note creation failed with duplication error even when there is no duplication: ${entryUri}`);
} }
return duplicate; return duplicate;
} }

View file

@ -141,26 +141,27 @@ export class ApPersonService implements OnModuleInit {
const expectHost = this.utilityService.punyHost(uri); const expectHost = this.utilityService.punyHost(uri);
if (!isActor(x)) { if (!isActor(x)) {
throw new UnrecoverableError(`invalid Actor type '${x.type}': ${uri}`); throw new UnrecoverableError(`invalid Actor type '${x.type}' in ${uri}`);
} }
if (!(typeof x.id === 'string' && x.id.length > 0)) { if (!(typeof x.id === 'string' && x.id.length > 0)) {
throw new UnrecoverableError(`invalid Actor - wrong id: ${uri}`); throw new UnrecoverableError(`invalid Actor ${uri} - wrong id type`);
} }
if (!(typeof x.inbox === 'string' && x.inbox.length > 0)) { if (!(typeof x.inbox === 'string' && x.inbox.length > 0)) {
throw new UnrecoverableError(`invalid Actor - wrong inbox: ${uri}`); throw new UnrecoverableError(`invalid Actor ${uri} - wrong inbox type`);
} }
if (this.utilityService.punyHost(x.inbox) !== expectHost) { const inboxHost = this.utilityService.punyHost(x.inbox);
throw new UnrecoverableError(`invalid Actor - inbox has different host: ${uri}`); if (inboxHost !== expectHost) {
throw new UnrecoverableError(`invalid Actor ${uri} - wrong inbox ${inboxHost}`);
} }
const sharedInboxObject = x.sharedInbox ?? (x.endpoints ? x.endpoints.sharedInbox : undefined); const sharedInboxObject = x.sharedInbox ?? (x.endpoints ? x.endpoints.sharedInbox : undefined);
if (sharedInboxObject != null) { if (sharedInboxObject != null) {
const sharedInbox = getApId(sharedInboxObject); const sharedInbox = getApId(sharedInboxObject);
if (!(typeof sharedInbox === 'string' && sharedInbox.length > 0 && this.utilityService.punyHost(sharedInbox) === expectHost)) { if (!(typeof sharedInbox === 'string' && sharedInbox.length > 0 && this.utilityService.punyHost(sharedInbox) === expectHost)) {
throw new Error('invalid Actor: wrong shared inbox'); throw new UnrecoverableError(`invalid Actor ${uri} - wrong shared inbox ${sharedInbox}`);
} }
} }
@ -170,16 +171,16 @@ export class ApPersonService implements OnModuleInit {
const collectionUri = getApId(xCollection); const collectionUri = getApId(xCollection);
if (typeof collectionUri === 'string' && collectionUri.length > 0) { if (typeof collectionUri === 'string' && collectionUri.length > 0) {
if (this.utilityService.punyHost(collectionUri) !== expectHost) { if (this.utilityService.punyHost(collectionUri) !== expectHost) {
throw new UnrecoverableError(`invalid Actor - ${collection} has different host: ${uri}`); throw new UnrecoverableError(`invalid Actor ${uri} - wrong ${collection} ${collectionUri}`);
} }
} else if (collectionUri != null) { } else if (collectionUri != null) {
throw new UnrecoverableError(`invalid Actor: wrong ${collection} in ${uri}`); throw new UnrecoverableError(`invalid Actor ${uri}: wrong ${collection} type`);
} }
} }
} }
if (!(typeof x.preferredUsername === 'string' && x.preferredUsername.length > 0 && x.preferredUsername.length <= 128 && /^\w([\w-.]*\w)?$/.test(x.preferredUsername))) { if (!(typeof x.preferredUsername === 'string' && x.preferredUsername.length > 0 && x.preferredUsername.length <= 128 && /^\w([\w-.]*\w)?$/.test(x.preferredUsername))) {
throw new UnrecoverableError(`invalid Actor - wrong username: ${uri}`); throw new UnrecoverableError(`invalid Actor ${uri} - wrong username`);
} }
// These fields are only informational, and some AP software allows these // These fields are only informational, and some AP software allows these
@ -187,7 +188,7 @@ export class ApPersonService implements OnModuleInit {
// we can at least see these users and their activities. // we can at least see these users and their activities.
if (x.name) { if (x.name) {
if (!(typeof x.name === 'string' && x.name.length > 0)) { if (!(typeof x.name === 'string' && x.name.length > 0)) {
throw new UnrecoverableError(`invalid Actor - wrong name: ${uri}`); throw new UnrecoverableError(`invalid Actor ${uri} - wrong name`);
} }
x.name = truncate(x.name, nameLength); x.name = truncate(x.name, nameLength);
} else if (x.name === '') { } else if (x.name === '') {
@ -196,24 +197,24 @@ export class ApPersonService implements OnModuleInit {
} }
if (x.summary) { if (x.summary) {
if (!(typeof x.summary === 'string' && x.summary.length > 0)) { if (!(typeof x.summary === 'string' && x.summary.length > 0)) {
throw new UnrecoverableError(`invalid Actor - wrong summary: ${uri}`); throw new UnrecoverableError(`invalid Actor ${uri} - wrong summary`);
} }
x.summary = truncate(x.summary, summaryLength); x.summary = truncate(x.summary, summaryLength);
} }
const idHost = this.utilityService.punyHost(x.id); const idHost = this.utilityService.punyHost(x.id);
if (idHost !== expectHost) { if (idHost !== expectHost) {
throw new UnrecoverableError(`invalid Actor - id has different host: ${uri}`); throw new UnrecoverableError(`invalid Actor ${uri} - wrong id ${x.id}`);
} }
if (x.publicKey) { if (x.publicKey) {
if (typeof x.publicKey.id !== 'string') { if (typeof x.publicKey.id !== 'string') {
throw new UnrecoverableError(`invalid Actor - publicKey.id is not a string: ${uri}`); throw new UnrecoverableError(`invalid Actor ${uri} - wrong publicKey.id type`);
} }
const publicKeyIdHost = this.utilityService.punyHost(x.publicKey.id); const publicKeyIdHost = this.utilityService.punyHost(x.publicKey.id);
if (publicKeyIdHost !== expectHost) { if (publicKeyIdHost !== expectHost) {
throw new UnrecoverableError(`invalid Actor - publicKey.id has different host: ${uri}`); throw new UnrecoverableError(`invalid Actor ${uri} - wrong publicKey.id ${x.publicKey.id}`);
} }
} }
@ -311,7 +312,7 @@ export class ApPersonService implements OnModuleInit {
if (resolver == null) resolver = this.apResolverService.createResolver(); if (resolver == null) resolver = this.apResolverService.createResolver();
const object = await resolver.resolve(uri); const object = await resolver.resolve(uri);
if (object.id == null) throw new UnrecoverableError(`null object.id: ${uri}`); if (object.id == null) throw new UnrecoverableError(`null object.id in ${uri}`);
const person = this.validateActor(object, uri); const person = this.validateActor(object, uri);
@ -348,11 +349,11 @@ export class ApPersonService implements OnModuleInit {
if (url != null) { if (url != null) {
if (!checkHttps(url)) { if (!checkHttps(url)) {
throw new UnrecoverableError(`unexpected schema of person url ${url}: ${uri}`); throw new UnrecoverableError(`unexpected schema of person url ${url} in ${uri}`);
} }
if (this.utilityService.punyHost(url) !== this.utilityService.punyHost(person.id)) { if (this.utilityService.punyHost(url) !== this.utilityService.punyHost(person.id)) {
throw new UnrecoverableError(`person url <> uri host mismatch: ${url} <> ${person.id}`); throw new UnrecoverableError(`person url <> uri host mismatch: ${url} <> ${person.id} in ${uri}`);
} }
} }
@ -561,11 +562,11 @@ export class ApPersonService implements OnModuleInit {
if (url != null) { if (url != null) {
if (!checkHttps(url)) { if (!checkHttps(url)) {
throw new UnrecoverableError(`unexpected schema of person url ${url}: ${uri}`); throw new UnrecoverableError(`unexpected schema of person url ${url} in ${uri}`);
} }
if (this.utilityService.punyHost(url) !== this.utilityService.punyHost(person.id)) { if (this.utilityService.punyHost(url) !== this.utilityService.punyHost(person.id)) {
throw new UnrecoverableError(`person url <> uri host mismatch: ${url} <> ${person.id}`); throw new UnrecoverableError(`person url <> uri host mismatch: ${url} <> ${person.id} in ${uri}`);
} }
} }
@ -732,7 +733,7 @@ export class ApPersonService implements OnModuleInit {
}); });
if (!collection) return; if (!collection) return;
if (!isCollectionOrOrderedCollection(collection)) throw new UnrecoverableError(`featured ${user.featured} is not Collection or OrderedCollection: ${user.uri}`); if (!isCollectionOrOrderedCollection(collection)) throw new UnrecoverableError(`featured ${user.featured} is not Collection or OrderedCollection in ${user.uri}`);
// Resolve to Object(may be Note) arrays // Resolve to Object(may be Note) arrays
const unresolvedItems = isCollection(collection) ? collection.items : collection.orderedItems; const unresolvedItems = isCollection(collection) ? collection.items : collection.orderedItems;

View file

@ -52,7 +52,7 @@ export class ApQuestionService {
if (!isQuestion(question)) throw new UnrecoverableError(`invalid type ${getApType(question)}: ${getNullableApId(question)}`); if (!isQuestion(question)) throw new UnrecoverableError(`invalid type ${getApType(question)}: ${getNullableApId(question)}`);
const multiple = question.oneOf === undefined; const multiple = question.oneOf === undefined;
if (multiple && question.anyOf === undefined) throw new Error(`invalid question - neither oneOf nor anyOf is defined: ${getNullableApId(question)}`); if (multiple && question.anyOf === undefined) throw new UnrecoverableError(`invalid question - neither oneOf nor anyOf is defined: ${getNullableApId(question)}`);
const expiresAt = question.endTime ? new Date(question.endTime) : question.closed ? new Date(question.closed) : null; const expiresAt = question.endTime ? new Date(question.endTime) : question.closed ? new Date(question.closed) : null;