Improve error handling of packaging functions

This commit is contained in:
syuilo 2018-10-05 01:43:47 +09:00
parent 401d0b1298
commit baf9b65801
No known key found for this signature in database
GPG key ID: BDC4C49D06AB9D69
6 changed files with 28 additions and 12 deletions

View file

@ -1,7 +1,7 @@
{ {
"name": "misskey", "name": "misskey",
"author": "syuilo <i@syuilo.com>", "author": "syuilo <i@syuilo.com>",
"version": "9.5.0", "version": "9.6.0",
"clientVersion": "1.0.10090", "clientVersion": "1.0.10090",
"codename": "nighthike", "codename": "nighthike",
"main": "./built/index.js", "main": "./built/index.js",

View file

@ -127,6 +127,15 @@ export async function deleteDriveFile(driveFile: string | mongo.ObjectID | IDriv
}); });
} }
export const packMany = async (
files: any[],
options?: {
detail: boolean
}
) => {
return (await Promise.all(files.map(f => pack(f, options)))).filter(x => x != null);
};
/** /**
* Pack a drive file for API response * Pack a drive file for API response
*/ */
@ -155,7 +164,11 @@ export const pack = (
_file = deepcopy(file); _file = deepcopy(file);
} }
if (!_file) return reject('invalid file arg.'); // (データベースの欠損などで)ファイルがデータベース上に見つからなかったとき
if (_file == null) {
console.warn(`in packaging driveFile: driveFile not found on database: ${_file}`);
return null;
}
// rendered target // rendered target
let _target: any = {}; let _target: any = {};

View file

@ -7,7 +7,7 @@ import { IUser, pack as packUser } from './user';
import { pack as packApp } from './app'; import { pack as packApp } from './app';
import PollVote, { deletePollVote } from './poll-vote'; import PollVote, { deletePollVote } from './poll-vote';
import Reaction, { deleteNoteReaction } from './note-reaction'; import Reaction, { deleteNoteReaction } from './note-reaction';
import { pack as packFile, IDriveFile } from './drive-file'; import { packMany as packFileMany, IDriveFile } from './drive-file';
import NoteWatching, { deleteNoteWatching } from './note-watching'; import NoteWatching, { deleteNoteWatching } from './note-watching';
import NoteReaction from './note-reaction'; import NoteReaction from './note-reaction';
import Favorite, { deleteFavorite } from './favorite'; import Favorite, { deleteFavorite } from './favorite';
@ -309,9 +309,7 @@ export const pack = async (
} }
// Populate files // Populate files
_note.files = Promise.all((_note.fileIds || []).map((fileId: mongo.ObjectID) => _note.files = packFileMany(_note.fileIds || []);
packFile(fileId)
));
// 後方互換性のため // 後方互換性のため
_note.mediaIds = _note.fileIds; _note.mediaIds = _note.fileIds;
@ -380,6 +378,12 @@ export const pack = async (
// resolve promises in _note object // resolve promises in _note object
_note = await rap(_note); _note = await rap(_note);
// (データベースの欠損などで)ユーザーがデータベース上に見つからなかったとき
if (_note.user == null) {
console.warn(`in packaging note: note user not found on database: note(${_note.id})`);
return null;
}
if (_note.user.isCat && _note.text) { if (_note.user.isCat && _note.text) {
_note.text = _note.text.replace(/な/g, 'にゃ').replace(/ナ/g, 'ニャ').replace(/ナ/g, 'ニャ'); _note.text = _note.text.replace(/な/g, 'にゃ').replace(/ナ/g, 'ニャ').replace(/ナ/g, 'ニャ');
} }

View file

@ -361,7 +361,7 @@ export const pack = (
_user = deepcopy(user); _user = deepcopy(user);
} }
// ユーザーがデータベース上に見つからなかったとき // (データベースの欠損などで)ユーザーがデータベース上に見つからなかったとき
if (_user == null) { if (_user == null) {
console.warn(`user not found on database: ${user}`); console.warn(`user not found on database: ${user}`);
return null; return null;

View file

@ -1,5 +1,5 @@
import $ from 'cafy'; import ID from '../../../../misc/cafy-id'; import $ from 'cafy'; import ID from '../../../../misc/cafy-id';
import DriveFile, { pack } from '../../../../models/drive-file'; import DriveFile, { packMany } from '../../../../models/drive-file';
import { ILocalUser } from '../../../../models/user'; import { ILocalUser } from '../../../../models/user';
export const meta = { export const meta = {
@ -73,6 +73,5 @@ export default async (params: any, user: ILocalUser) => {
}); });
// Serialize // Serialize
const _files = await Promise.all(files.map(file => pack(file))); return await packMany(files);
return _files;
}; };

View file

@ -1,5 +1,5 @@
import $ from 'cafy'; import ID from '../../../../misc/cafy-id'; import $ from 'cafy'; import ID from '../../../../misc/cafy-id';
import DriveFile, { pack } from '../../../../models/drive-file'; import DriveFile, { packMany } from '../../../../models/drive-file';
import { ILocalUser } from '../../../../models/user'; import { ILocalUser } from '../../../../models/user';
export const meta = { export const meta = {
@ -63,5 +63,5 @@ export default (params: any, user: ILocalUser) => new Promise(async (res, rej) =
}); });
// Serialize // Serialize
res(await Promise.all(files.map(file => pack(file)))); res(await packMany(files));
}); });